From 5559491b8371d4a072571e85388d42bc4fe4de60 Mon Sep 17 00:00:00 2001 From: Quint Daenen Date: Fri, 3 May 2024 09:41:24 +0200 Subject: [PATCH] Close instances after tests. --- pocketic/http.go | 2 +- pocketic/pocketic_test.go | 35 +++++++++++++++++++++-------------- pocketic/server.go | 8 ++++++-- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/pocketic/http.go b/pocketic/http.go index 5d62c47..7a109ed 100644 --- a/pocketic/http.go +++ b/pocketic/http.go @@ -99,7 +99,7 @@ func (pic PocketIC) MakeLive(port *int) (string, error) { var resp CreateHttpGatewayResponse if err := pic.do( http.MethodPost, - fmt.Sprintf("http://127.0.0.1:%d/http_gateway", pic.server.port), + fmt.Sprintf("%s/http_gateway", pic.server.URL()), http.StatusCreated, HttpGatewayConfig{ ListenAt: port, diff --git a/pocketic/pocketic_test.go b/pocketic/pocketic_test.go index 98b2848..6af1f33 100644 --- a/pocketic/pocketic_test.go +++ b/pocketic/pocketic_test.go @@ -16,17 +16,11 @@ import ( "testing" ) -func TestPocketIC_CreateCanister(t *testing.T) { +func CreateCanister(t *testing.T) *pocketic.PocketIC { pic, err := pocketic.New(pocketic.WithLogger(new(testLogger))) if err != nil { t.Fatal(err) } - defer func() { - err := pic.Close() - if err != nil { - t.Fatal(err) - } - }() canisterID, err := pic.CreateCanister() if err != nil { @@ -35,9 +29,11 @@ func TestPocketIC_CreateCanister(t *testing.T) { if _, err := pic.AddCycles(*canisterID, 2_000_000_000_000); err != nil { t.Fatal(err) } + + return pic } -func TestPocketIC_MakeLive(t *testing.T) { +func MakeLive(t *testing.T) *pocketic.PocketIC { pic, err := pocketic.New( pocketic.WithLogger(new(testLogger)), pocketic.WithNNSSubnet(), @@ -46,12 +42,6 @@ func TestPocketIC_MakeLive(t *testing.T) { if err != nil { t.Fatal(err) } - defer func() { - err := pic.Close() - if err != nil { - t.Fatal(err) - } - }() endpoint, err := pic.MakeLive(nil) if err != nil { @@ -102,6 +92,23 @@ func TestPocketIC_MakeLive(t *testing.T) { }); err != nil { t.Fatal(err) } + + return pic +} + +func TestPocketIC(t *testing.T) { + var instances []*pocketic.PocketIC + t.Run("CreateCanister", func(t *testing.T) { + instances = append(instances, CreateCanister(t)) + }) + t.Run("MakeLive", func(t *testing.T) { + instances = append(instances, MakeLive(t)) + }) + for _, i := range instances { + if err := i.Close(); err != nil { + t.Fatal(err) + } + } } func compileMotoko(t *testing.T, in, out string) { diff --git a/pocketic/server.go b/pocketic/server.go index 9493e52..87e6f97 100644 --- a/pocketic/server.go +++ b/pocketic/server.go @@ -48,19 +48,23 @@ func newServer(opts ...serverOption) (*server, error) { return nil, fmt.Errorf("unsupported pocket-ic version, must be v3.x: %s", version) } - pid := os.Getpid() + pid := os.Getppid() cmdArgs := []string{"--pid", strconv.Itoa(pid)} if config.ttl != nil { cmdArgs = append(cmdArgs, "--ttl", strconv.Itoa(*config.ttl)) } cmd := exec.Command(binPath, cmdArgs...) + if os.Getenv("POCKET_IC_MUTE_SERVER") == "" { + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + } if err := cmd.Start(); err != nil { return nil, fmt.Errorf("failed to start pocket-ic: %v", err) } tmpDir := os.TempDir() readyFile := path.Join(tmpDir, fmt.Sprintf("pocket_ic_%d.ready", pid)) - stopAt := time.Now().Add(10 * time.Second) + stopAt := time.Now().Add(5 * time.Second) for _, err := os.Stat(readyFile); os.IsNotExist(err); _, err = os.Stat(readyFile) { time.Sleep(100 * time.Millisecond) if time.Now().After(stopAt) {