Skip to content

Commit

Permalink
fixed flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
sha1n committed Sep 22, 2023
1 parent e41ae38 commit 2f102e3
Showing 1 changed file with 61 additions and 36 deletions.
97 changes: 61 additions & 36 deletions spinner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package termite

import (
"bytes"
"io"
"log"
"regexp"
"strings"
"testing"
"time"
Expand All @@ -15,28 +18,29 @@ const (
)

func TestSpinnerCharSequence(t *testing.T) {
emulatedStdout := new(bytes.Buffer)
reader, writer := io.Pipe()

spinner := NewSpinner(emulatedStdout, "", interval, DefaultSpinnerFormatter())
spinner := NewSpinner(writer, "", interval, DefaultSpinnerFormatter())
cancel, err := spinner.Start()
defer cancel()

assert.NoError(t, err)
assert.NotNil(t, cancel)

assertSpinnerCharSequence(t, emulatedStdout)
assertSpinnerCharSequence(t, reader)
}

func TestSpinnerCancellation(t *testing.T) {
emulatedStdout := new(bytes.Buffer)
reader, writer := io.Pipe()

spin := NewSpinner(emulatedStdout, "", interval, DefaultSpinnerFormatter())
cancel, _ := spin.Start()
spin := NewSpinner(writer, "", interval, DefaultSpinnerFormatter())
cancel, err := spin.Start()

assertSpinnerCharSequence(t, emulatedStdout)
assert.NoError(t, err)
assertSpinnerCharSequence(t, reader)

cancel()
assertStoppedEventually(t, emulatedStdout, spin.(*spinner))
assertStoppedEventually(t, reader, spin.(*spinner))
}

func TestSpinnerStartAlreadyRunning(t *testing.T) {
Expand Down Expand Up @@ -132,53 +136,74 @@ func bufferContains(outBuffer *bytes.Buffer, expected string) func() bool {
}
}

func assertStoppedEventually(t *testing.T, outBuffer *bytes.Buffer, spinner *spinner) {
func assertStoppedEventually(t *testing.T, outBuffer *io.PipeReader, spinner *spinner) {
assert.Eventually(
t,
func() bool { return !spinner.isActiveSafe() },
spinner.isActiveSafe,
timeout,
interval,
"expected spinner to deactivate",
)

outBuffer.Reset() // clear the buffer

hasNoMoreOutput := func() bool {
buffer := make([]byte, 1024)
timer := time.NewTimer(100 * spinner.interval)
defer timer.Stop()

log.Println("XXX: hasNoMoreOutput")

for {
select {
case <-timer.C:
log.Println("No output for 10 intervals spinner intervals!")
return true
default:
n, err := outBuffer.Read(buffer)
if err == io.EOF {
return true
} else if err != nil {
log.Fatal(err)
}
return n == 0
}
}
}

assert.Eventually(
t,
func() bool { return outBuffer.UnreadByte() != nil },
hasNoMoreOutput,
timeout,
spinner.interval,
"expected no more output from spinner",
)
}

func assertSpinnerCharSequence(t *testing.T, outBuffer *bytes.Buffer) {
func assertSpinnerCharSequence(t *testing.T, outBuffer *io.PipeReader) {
charSeq := DefaultSpinnerCharSeq()
readChars := []string{}
readBytes := make([]byte, 1024)
readStrings := []string{}

scan := func() {
for {
r, _, e := outBuffer.ReadRune()
print(string(r), ",")
if e != nil {
continue
}
readChar := string(r)
if len(readChars) == 0 && readChar == charSeq[0] {
readChars = append(readChars, readChar)
} else if len(readChars) > 0 {
for _, ch := range charSeq {
if ch == readChar {
readChars = append(readChars, ch)
}

if len(readChars) == len(charSeq) {
return
}
}
for {
size, e := outBuffer.Read(readBytes)
if e != nil || size == 0 {
continue
}

str := stripControlCharacters(string(readBytes[:size]))
if str != "" {
readStrings = append(readStrings, str)
if len(readStrings) == len(charSeq)*2 {
break
}
}
}

scan()
assert.Contains(t, strings.Join(readStrings, ""), strings.Join(charSeq, ""))
}

func stripControlCharacters(input string) string {
controlCharsRegex := regexp.MustCompile(`[[:cntrl:]]|\[|K`)

assert.Equal(t, charSeq, readChars)
return controlCharsRegex.ReplaceAllString(input, "")
}

0 comments on commit 2f102e3

Please sign in to comment.