diff options
| author | Paul Buetow <paul@buetow.org> | 2021-10-11 11:51:30 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2021-10-11 11:52:30 +0300 |
| commit | c0f4ebc9b3773c37e22c686024c8cc7ce4e71f9f (patch) | |
| tree | 300cba741e332ffb34eb1bd3df5c03fdb9e1013d /integrationtests | |
| parent | 71f89dc7ec7cf993d1eca98771212afe6310e9c8 (diff) | |
add dtail integration test
Diffstat (limited to 'integrationtests')
| -rw-r--r-- | integrationtests/dtail_test.go | 103 | ||||
| -rw-r--r-- | integrationtests/dtailhealthcheck_test.go | 8 |
2 files changed, 71 insertions, 40 deletions
diff --git a/integrationtests/dtail_test.go b/integrationtests/dtail_test.go index 267cd26..79b5881 100644 --- a/integrationtests/dtail_test.go +++ b/integrationtests/dtail_test.go @@ -2,18 +2,31 @@ package integrationtests import ( "context" + "fmt" "os" + "strings" "testing" + "time" ) +// TODO: Have a serverless variant too. func TestDTailWithServer(t *testing.T) { followFile := "dtail.follow.tmp" - //serverStdoutFile := "dtail.dserver.stdout.tmp" - //greetings := []string{"world", "sol system", "milky way", "universe", "multiverse"} + greetings := []string{"world!", "sol-system!", "milky-way!", "universe!", "multiverse!"} ctx, cancel := context.WithCancel(context.Background()) defer cancel() + go func() { + select { + case <-time.After(time.Minute): + t.Error("Max time for this test exceeded!") + cancel() + case <-ctx.Done(): + return + } + }() + serverCh, _, _, err := startCommand(ctx, "../dserver", "--logger", "stdout", @@ -40,55 +53,69 @@ func TestDTailWithServer(t *testing.T) { t.Error(err) return } + // Write greetings to followFile + fd, err := os.Create(followFile) + if err != nil { + t.Error(err) + } + defer fd.Close() + + go func() { + var circular int + for { + select { + case <-time.After(time.Second): + fd.WriteString(time.Now().String()) + fd.WriteString(fmt.Sprintf(" - Hello %s\n", greetings[circular])) + circular = (circular + 1) % len(greetings) + case <-ctx.Done(): + return + } + } + }() + + var greetingsRecv []string - for { + for len(greetingsRecv) < len(greetings) { select { case line := <-serverCh: t.Log("server:", line) case line := <-clientCh: t.Log("client:", line) + if strings.Contains(line, "Hello ") { + s := strings.Split(line, " ") + greeting := s[len(s)-1] + greetingsRecv = append(greetingsRecv, greeting) + t.Log("Received greeting", greeting, len(greetingsRecv)) + } case <-ctx.Done(): t.Log("Done reading client and server pipes") } } - /* - // Start dtail client, connect to the server and follow followFile. - - //clientStdoutFile := "dtail.stdout.tmp" - /* - - t.Log(clientArgs) - // TODO: Pipe with dtail command to read stdin stream. - // runCommandContextRetry(ctx, t, "../dtail", clientArgs, clientStdoutFile) - - // Write greetings to followFile - fd, err := os.Create(followFile) - if err != nil { - t.Error(err) - } - defer fd.Close() + // We expect to have received the greetings in the same order they were sent.` + offset := -1 + for i, g := range greetings { + if g == greetingsRecv[0] { + offset = i + break + } + } + if offset == -1 { + t.Error("Could not find first offset of greetings received") + return + } - go func() { - var circular int - for { - select { - case <-ctx.Done(): - return - case <-time.After(time.Second): - fd.WriteString(time.Now().String()) - fd.WriteString(fmt.Sprintf(" - Hello %s!\n", greetings[circular])) - circular = (circular + 1) % len(greetings) - } - } - }() - */ + for i, g := range greetingsRecv { + index := (i + offset) % len(greetings) + if greetings[index] != g { + t.Error(fmt.Sprintf("Expected '%s' but got '%s' at '%v' vs '%v'\n", + g, greetings[index], greetings, greetingsRecv)) + return + } + } - /* - os.Remove(serverStdoutFile) - os.Remove(clientStdoutFile) - os.Remove(followFile) - */ + os.Remove(followFile) } func TestDTailColorTable(t *testing.T) { diff --git a/integrationtests/dtailhealthcheck_test.go b/integrationtests/dtailhealthcheck_test.go index a99bfdc..bb6c146 100644 --- a/integrationtests/dtailhealthcheck_test.go +++ b/integrationtests/dtailhealthcheck_test.go @@ -53,14 +53,18 @@ func TestDTailHealthCheck3(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - startCommand(ctx, + _, _, _, err := startCommand(ctx, "../dserver", "--logger", "stdout", "--logLevel", "trace", "--port", "4242", ) + if err != nil { + t.Error(err) + return + } - _, err := runCommandRetry(ctx, 10, stdoutFile, + _, err = runCommandRetry(ctx, 10, stdoutFile, "../dtailhealthcheck", "--server", "localhost:4242") if err != nil { t.Error(err) |
