Skip to content

Commit

Permalink
Remove blocking os.Std* pipes.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed May 6, 2024
1 parent 27da439 commit d9571ce
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 52 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 0 additions & 5 deletions ic/ic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions pocketic/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -228,6 +228,4 @@ func Endpoints(t *testing.T) *pocketic.PocketIC {
}
})
})

return pic
}
1 change: 0 additions & 1 deletion pocketic/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func (pic PocketIC) GetInstances() ([]string, error) {
); err != nil {
return nil, err
}
fmt.Println(instances)
return instances, nil
}

Expand Down
5 changes: 0 additions & 5 deletions pocketic/pocketic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 3 additions & 29 deletions pocketic/pocketic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions pocketic/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit d9571ce

Please sign in to comment.