From d9571cee0ed15c733d887797f00b6c6a74548457 Mon Sep 17 00:00:00 2001 From: Quint Daenen Date: Mon, 6 May 2024 12:31:36 +0200 Subject: [PATCH] Remove blocking os.Std* pipes. --- README.md | 1 + ic/ic_test.go | 5 ----- pocketic/endpoints_test.go | 4 +--- pocketic/instances.go | 1 - pocketic/pocketic.go | 5 ----- pocketic/pocketic_test.go | 32 +++----------------------------- pocketic/server.go | 18 +++++++++--------- 7 files changed, 14 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index e7226f2..392b1fe 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ You can find the documentation for each package in the links below. Examples can | `gen` | [![DOC](https://img.shields.io/badge/-DOC-blue)](https://pkg.go.dev/github.com/aviate-labs/agent-go/gen) | A library to generate Golang clients. | | `ic` | [![DOC](https://img.shields.io/badge/-DOC-blue)](https://pkg.go.dev/github.com/aviate-labs/agent-go/ic) | Multiple auto-generated sub-modules to talk to the Internet Computer services | | `identity` | [![DOC](https://img.shields.io/badge/-DOC-blue)](https://pkg.go.dev/github.com/aviate-labs/agent-go/identity) | A library that creates/manages identities. | +| `pocketic` | [![DOC](https://img.shields.io/badge/-DOC-blue)](https://pkg.go.dev/github.com/aviate-labs/agent-go/pocketic) | A client library to talk to the PocketIC Server. | | `principal` | [![DOC](https://img.shields.io/badge/-DOC-blue)](https://pkg.go.dev/github.com/aviate-labs/agent-go/principal) | Generic Identifiers for the Internet Computer | More dependencies in the [go.mod](./go.mod) file. diff --git a/ic/ic_test.go b/ic/ic_test.go index 4d599be..d002908 100644 --- a/ic/ic_test.go +++ b/ic/ic_test.go @@ -21,11 +21,6 @@ func TestModules(t *testing.T) { if err != nil { t.Skip(err) } - defer func() { - if err := pic.Close(); err != nil { - t.Error(err) - } - }() rawHost, err := pic.MakeLive(nil) if err != nil { diff --git a/pocketic/endpoints_test.go b/pocketic/endpoints_test.go index d55f7b3..7c44b9c 100644 --- a/pocketic/endpoints_test.go +++ b/pocketic/endpoints_test.go @@ -9,7 +9,7 @@ import ( "testing" ) -func Endpoints(t *testing.T) *pocketic.PocketIC { +func TestEndpoints(t *testing.T) { pic, err := pocketic.New( pocketic.WithLogger(new(testLogger)), pocketic.WithNNSSubnet(), @@ -228,6 +228,4 @@ func Endpoints(t *testing.T) *pocketic.PocketIC { } }) }) - - return pic } diff --git a/pocketic/instances.go b/pocketic/instances.go index 3cbc45e..2586686 100644 --- a/pocketic/instances.go +++ b/pocketic/instances.go @@ -59,7 +59,6 @@ func (pic PocketIC) GetInstances() ([]string, error) { ); err != nil { return nil, err } - fmt.Println(instances) return instances, nil } diff --git a/pocketic/pocketic.go b/pocketic/pocketic.go index 6b8c06e..5d30d48 100644 --- a/pocketic/pocketic.go +++ b/pocketic/pocketic.go @@ -250,11 +250,6 @@ func New(opts ...Option) (*PocketIC, error) { }, nil } -// Close closes the PocketIC client. -func (pic *PocketIC) Close() error { - return pic.server.Close() -} - // InstanceURL returns the URL of the PocketIC instance. func (pic PocketIC) InstanceURL() string { return fmt.Sprintf("%s/instances/%d", pic.server.URL(), pic.InstanceID) diff --git a/pocketic/pocketic_test.go b/pocketic/pocketic_test.go index 973500b..4466297 100644 --- a/pocketic/pocketic_test.go +++ b/pocketic/pocketic_test.go @@ -17,7 +17,7 @@ import ( "testing" ) -func ConcurrentCalls(t *testing.T) *pocketic.PocketIC { +func TestConcurrentCalls(t *testing.T) { pic, err := pocketic.New() if err != nil { t.Fatal(err) @@ -41,10 +41,9 @@ func ConcurrentCalls(t *testing.T) *pocketic.PocketIC { }() } wg.Wait() - return pic } -func CreateCanister(t *testing.T) *pocketic.PocketIC { +func TestCreateCanister(t *testing.T) { pic, err := pocketic.New(pocketic.WithLogger(new(testLogger))) if err != nil { t.Fatal(err) @@ -57,11 +56,9 @@ func CreateCanister(t *testing.T) *pocketic.PocketIC { if _, err := pic.AddCycles(*canisterID, 2_000_000_000_000); err != nil { t.Fatal(err) } - - return pic } -func HttpGateway(t *testing.T) *pocketic.PocketIC { +func TestHttpGateway(t *testing.T) { pic, err := pocketic.New( pocketic.WithLogger(new(testLogger)), pocketic.WithNNSSubnet(), @@ -133,29 +130,6 @@ func HttpGateway(t *testing.T) *pocketic.PocketIC { if err := pic.MakeDeterministic(); 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("HttpGateway", func(t *testing.T) { - instances = append(instances, HttpGateway(t)) - }) - t.Run("ConcurrentCalls", func(t *testing.T) { - instances = append(instances, ConcurrentCalls(t)) - }) - t.Run("Endpoints", func(t *testing.T) { - instances = append(instances, Endpoints(t)) - }) - for _, i := range instances { - if err := i.Close(); err != nil { - t.Fatal(err) - } - } } func compileMotoko(t *testing.T, in, out string) []byte { diff --git a/pocketic/server.go b/pocketic/server.go index 87e6f97..cde7a85 100644 --- a/pocketic/server.go +++ b/pocketic/server.go @@ -11,6 +11,13 @@ import ( "time" ) +type printWriter struct{} + +func (w printWriter) Write(p []byte) (n int, err error) { + fmt.Print(string(p)) + return len(p), nil +} + type server struct { port int cmd *exec.Cmd @@ -55,8 +62,8 @@ func newServer(opts ...serverOption) (*server, error) { } cmd := exec.Command(binPath, cmdArgs...) if os.Getenv("POCKET_IC_MUTE_SERVER") == "" { - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + cmd.Stdout = new(printWriter) + cmd.Stderr = new(printWriter) } if err := cmd.Start(); err != nil { return nil, fmt.Errorf("failed to start pocket-ic: %v", err) @@ -92,13 +99,6 @@ func newServer(opts ...serverOption) (*server, error) { }, nil } -func (s server) Close() error { - if err := s.cmd.Process.Kill(); err != nil { - return fmt.Errorf("failed to kill pocket-ic: %v", err) - } - return nil -} - func (s server) URL() string { return fmt.Sprintf("http://127.0.0.1:%d", s.port) }