Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Sep 13, 2024
1 parent 36c5780 commit f305b10
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
56 changes: 56 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package discovery

import (
"fmt"
"io"
"net"
"testing"
"time"
Expand Down Expand Up @@ -93,3 +94,58 @@ func TestDiscoveryStdioHandling(t *testing.T) {

require.False(t, disc.Alive())
}

func TestClient(t *testing.T) {
// Build dummy-discovery
builder, err := paths.NewProcess(nil, "go", "build")
require.NoError(t, err)
builder.SetDir("dummy-discovery")
require.NoError(t, builder.Run())

t.Run("WithDiscoveryCrashingOnStartup", func(t *testing.T) {
// Run client with discovery crashing on startup
cl := NewClient("1", "dummy-discovery/dummy-discovery", "--invalid")
require.ErrorIs(t, cl.Run(), io.EOF)
})

t.Run("WithDiscoveryCrashingWhileSendingCommands", func(t *testing.T) {
// Run client with crashing discovery after 1 second
cl := NewClient("1", "dummy-discovery/dummy-discovery", "-k")
require.NoError(t, cl.Run())

time.Sleep(time.Second)

ch, err := cl.StartSync(20)
require.Error(t, err)
require.Nil(t, ch)
})

t.Run("WithDiscoveryCrashingWhileStreamingEvents", func(t *testing.T) {
// Run client with crashing discovery after 1 second
cl := NewClient("1", "dummy-discovery/dummy-discovery", "-k")
require.NoError(t, cl.Run())

ch, err := cl.StartSync(20)
require.NoError(t, err)

time.Sleep(time.Second)

loop:
for {
select {
case msg, ok := <-ch:
if !ok {
// Channel closed: Test passed
fmt.Println("Event channel closed")
break loop
}
fmt.Println("Recv: ", msg)
case <-time.After(time.Second):
t.Error("Crashing client did not close event channel")
break loop
}
}

cl.Quit()
})
}
9 changes: 9 additions & 0 deletions dummy-discovery/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package args
import (
"fmt"
"os"
"time"
)

// Tag is the current git tag
Expand All @@ -38,6 +39,14 @@ func Parse() {
fmt.Printf("dummy-discovery %s (build timestamp: %s)\n", Tag, Timestamp)
os.Exit(0)
}
if arg == "-k" {
// Emulate crashing discovery
go func() {
time.Sleep(time.Millisecond * 500)
os.Exit(1)
}()
continue
}
fmt.Fprintf(os.Stderr, "invalid argument: %s\n", arg)
os.Exit(1)
}
Expand Down

0 comments on commit f305b10

Please sign in to comment.