ScuttleBot

scuttlebot / pkg / client / client_test.go
Source Blame History 113 lines
7afa71e… lmata 1 package client_test
7afa71e… lmata 2
7afa71e… lmata 3 import (
7afa71e… lmata 4 "context"
7afa71e… lmata 5 "testing"
7afa71e… lmata 6 "time"
7afa71e… lmata 7
7afa71e… lmata 8 "github.com/conflicthq/scuttlebot/pkg/client"
7afa71e… lmata 9 "github.com/conflicthq/scuttlebot/pkg/protocol"
7afa71e… lmata 10 )
7afa71e… lmata 11
7afa71e… lmata 12 func TestNewValidation(t *testing.T) {
7afa71e… lmata 13 tests := []struct {
7afa71e… lmata 14 name string
7afa71e… lmata 15 opts client.Options
7afa71e… lmata 16 }{
7afa71e… lmata 17 {"missing server", client.Options{Nick: "a", Password: "p"}},
7afa71e… lmata 18 {"missing nick", client.Options{ServerAddr: "localhost:6667", Password: "p"}},
7afa71e… lmata 19 {"missing password", client.Options{ServerAddr: "localhost:6667", Nick: "a"}},
7afa71e… lmata 20 }
7afa71e… lmata 21 for _, tt := range tests {
7afa71e… lmata 22 t.Run(tt.name, func(t *testing.T) {
7afa71e… lmata 23 if _, err := client.New(tt.opts); err == nil {
7afa71e… lmata 24 t.Error("expected error, got nil")
7afa71e… lmata 25 }
7afa71e… lmata 26 })
7afa71e… lmata 27 }
7afa71e… lmata 28 }
7afa71e… lmata 29
7afa71e… lmata 30 func TestNewOK(t *testing.T) {
7afa71e… lmata 31 c, err := client.New(client.Options{
7afa71e… lmata 32 ServerAddr: "localhost:6667",
7afa71e… lmata 33 Nick: "agent-01",
7afa71e… lmata 34 Password: "secret",
7afa71e… lmata 35 })
7afa71e… lmata 36 if err != nil {
7afa71e… lmata 37 t.Fatalf("unexpected error: %v", err)
7afa71e… lmata 38 }
7afa71e… lmata 39 if c == nil {
7afa71e… lmata 40 t.Fatal("expected non-nil client")
7afa71e… lmata 41 }
7afa71e… lmata 42 }
7afa71e… lmata 43
7afa71e… lmata 44 func TestHandleRegistration(t *testing.T) {
7afa71e… lmata 45 c, _ := client.New(client.Options{
7afa71e… lmata 46 ServerAddr: "localhost:6667",
7afa71e… lmata 47 Nick: "agent-01",
7afa71e… lmata 48 Password: "secret",
7afa71e… lmata 49 })
7afa71e… lmata 50
7afa71e… lmata 51 called := make(chan string, 1)
7afa71e… lmata 52 c.Handle("task.create", func(ctx context.Context, env *protocol.Envelope) error {
7afa71e… lmata 53 called <- env.Type
7afa71e… lmata 54 return nil
7afa71e… lmata 55 })
7afa71e… lmata 56
7afa71e… lmata 57 // Verify handler is registered by checking it doesn't panic.
7afa71e… lmata 58 // Dispatch is tested indirectly via the exported Handle API.
7afa71e… lmata 59 select {
7afa71e… lmata 60 case <-called:
7afa71e… lmata 61 t.Error("handler fired unexpectedly before any message")
7afa71e… lmata 62 default:
7afa71e… lmata 63 }
7afa71e… lmata 64 }
7afa71e… lmata 65
7afa71e… lmata 66 func TestSendNotConnected(t *testing.T) {
7afa71e… lmata 67 c, _ := client.New(client.Options{
7afa71e… lmata 68 ServerAddr: "localhost:6667",
7afa71e… lmata 69 Nick: "agent-01",
7afa71e… lmata 70 Password: "secret",
7afa71e… lmata 71 })
7afa71e… lmata 72
7afa71e… lmata 73 err := c.Send(context.Background(), "#fleet", "task.create", map[string]string{"task": "x"})
7afa71e… lmata 74 if err == nil {
7afa71e… lmata 75 t.Error("expected error when not connected, got nil")
7afa71e… lmata 76 }
7afa71e… lmata 77 }
7afa71e… lmata 78
7afa71e… lmata 79 func TestRunCancelledImmediately(t *testing.T) {
7afa71e… lmata 80 c, _ := client.New(client.Options{
7afa71e… lmata 81 ServerAddr: "localhost:6667",
7afa71e… lmata 82 Nick: "agent-01",
7afa71e… lmata 83 Password: "secret",
7afa71e… lmata 84 })
7afa71e… lmata 85
7afa71e… lmata 86 ctx, cancel := context.WithCancel(context.Background())
7afa71e… lmata 87 cancel() // cancel immediately
7afa71e… lmata 88
7afa71e… lmata 89 done := make(chan error, 1)
7afa71e… lmata 90 go func() { done <- c.Run(ctx) }()
7afa71e… lmata 91
7afa71e… lmata 92 select {
7afa71e… lmata 93 case err := <-done:
7afa71e… lmata 94 if err != nil {
7afa71e… lmata 95 t.Errorf("Run returned error: %v", err)
7afa71e… lmata 96 }
7afa71e… lmata 97 case <-time.After(2 * time.Second):
7afa71e… lmata 98 t.Error("Run did not return after ctx cancelled")
7afa71e… lmata 99 }
7afa71e… lmata 100 }
7afa71e… lmata 101
7afa71e… lmata 102 func TestWildcardHandler(t *testing.T) {
7afa71e… lmata 103 // Verify that registering "*" doesn't panic and multiple handlers stack.
7afa71e… lmata 104 c, _ := client.New(client.Options{
7afa71e… lmata 105 ServerAddr: "localhost:6667",
7afa71e… lmata 106 Nick: "agent-01",
7afa71e… lmata 107 Password: "secret",
7afa71e… lmata 108 })
7afa71e… lmata 109
7afa71e… lmata 110 c.Handle("*", func(ctx context.Context, env *protocol.Envelope) error { return nil })
7afa71e… lmata 111 c.Handle("*", func(ctx context.Context, env *protocol.Envelope) error { return nil })
7afa71e… lmata 112 c.Handle("task.create", func(ctx context.Context, env *protocol.Envelope) error { return nil })
7afa71e… lmata 113 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button