Skip to content
This repository has been archived by the owner on Feb 17, 2024. It is now read-only.

Commit

Permalink
Test spawning using the host network
Browse files Browse the repository at this point in the history
Signed-off-by: Chris O'Hara <[email protected]>
  • Loading branch information
chriso committed Jul 12, 2023
1 parent 24dd255 commit e1a4ff2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
11 changes: 11 additions & 0 deletions replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,15 @@ var replay = tests{
assert.Equal(t, exitCode, 0)
assert.Equal(t, replay, stdout)
},

"guests can spawn processes": func(t *testing.T) {
stdout, stderr, exitCode := timecraft(t, "run", "--", "./testdata/go/spawn.wasm")
assert.Equal(t, exitCode, 0)

processID, _, _ := strings.Cut(stderr, "\n")

replay, _, exitCode := timecraft(t, "replay", strings.TrimSpace(processID))
assert.Equal(t, exitCode, 0)
assert.Equal(t, replay, stdout)
},
}
31 changes: 20 additions & 11 deletions testdata/go/spawn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"net"
"net/http"
"net/netip"
"net/url"
"os"
"time"
Expand All @@ -16,6 +15,8 @@ import (
"github.com/stealthrocket/timecraft/sdk/go/timecraft"
)

const workerPort = 10948

func main() {
var err error
switch {
Expand Down Expand Up @@ -49,14 +50,23 @@ func supervisor(ctx context.Context) error {
fmt.Printf("executing as process %s in timecraft %s\n", processID, version)

// Spawn the same WASM module, but with the "worker" arg.
workerModule := timecraft.ModuleSpec{Args: []string{"worker"}}
workerModule := timecraft.ModuleSpec{
Args: []string{"worker"},
Capabilities: timecraft.HostNetworkingCapability,
}

workerID, workerAddr, err := client.Spawn(ctx, workerModule)
workerID, _, err := client.Spawn(ctx, workerModule)
if err != nil {
return fmt.Errorf("failed to spawn worker: %w", err)
}
defer client.Kill(ctx, workerID)

// FIXME: the retry loop doesn't currently work. The sandbox seems to say that
// the connection succeeds, but then on fd_read it returns ECONNREFUSED??
time.Sleep(2 * time.Second)

workerAddr := fmt.Sprintf("127.0.0.1:%d", workerPort)

fmt.Printf("connecting to worker process %s on address %s\n", workerID, workerAddr)

httpClient := &http.Client{
Expand All @@ -77,7 +87,7 @@ func supervisor(ctx context.Context) error {
},
}

u, _ := url.Parse(fmt.Sprintf("http://%s/", netip.AddrPortFrom(workerAddr, 3000)))
u, _ := url.Parse(fmt.Sprintf("http://%s/", workerAddr))
req := &http.Request{
Method: "GET",
URL: u,
Expand All @@ -89,19 +99,18 @@ func supervisor(ctx context.Context) error {
}

fmt.Printf("worker responded with %d\n", res.StatusCode)

if res.StatusCode != 200 {
return fmt.Errorf("unexpected worker status code: %d", res.StatusCode)
}
return nil
}

func worker() error {
defer fmt.Println("worker exiting")

addr := ":3000"
listener, err := wasip1.Listen("tcp", addr)
workerAddr := fmt.Sprintf(":%d", workerPort)
listener, err := wasip1.Listen("tcp", workerAddr)
if err != nil {
return fmt.Errorf("failed to listen on %s: %w", addr, err)
return fmt.Errorf("failed to listen on %s: %w", workerAddr, err)
}
fmt.Println("worker listening on", addr)
return http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))
Expand Down

0 comments on commit e1a4ff2

Please sign in to comment.