Skip to content

Commit

Permalink
Close instances after tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed May 3, 2024
1 parent 01381a6 commit 5559491
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pocketic/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 21 additions & 14 deletions pocketic/pocketic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions pocketic/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5559491

Please sign in to comment.