Skip to content

Commit

Permalink
Make TailTest less flaky (#2)
Browse files Browse the repository at this point in the history
This change accounts for the fact that Tail.Stop() and the events from
inotify have no way of being synchronized, so the test now allows for
all the observable variants.

It also runs the test 100 times just to make sure it's not flaky in the
future.
  • Loading branch information
lhchavez authored May 24, 2020
1 parent 628d951 commit 76ea01a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ jobs:
run: golint -set_exit_status ./...

- name: Get dependencies
run: go get -tags=static -t -v ./...
run: go get -t -v ./...

- name: Vet
run: go vet -tags=static -v ./...
run: go vet -v ./...

- name: Test
run: go test -tags=static -v -race -coverprofile=coverage.txt -covermode=atomic ./...
run: go test -count=100 -v -race -coverprofile=coverage.txt -covermode=atomic ./...

- name: Ensure formatting
run: |
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
id: go

- name: Get dependencies
run: go get -tags=static -t -v ./...
run: go get -t -v ./...

- name: Bump version and push tag
id: bump-version
Expand All @@ -37,7 +37,6 @@ jobs:
mkdir -p artifacts/usr/bin/
go build -o artifacts/usr/bin/omegaup-logslurp \
-ldflags "-X main.ProgramVersion=${{ steps.bump-version.outputs.tag }}" \
-tags=static \
github.com/omegaup/logslurp/cmd/omegaup-logslurp
- name: Package
Expand Down
34 changes: 27 additions & 7 deletions tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,11 @@ func TestTail(t *testing.T) {
originalInode := tail.Inode()

doneChan := make(chan struct{})
var buf bytes.Buffer
go func() {
var buf bytes.Buffer
if _, err := io.Copy(&buf, tail); err != nil {
t.Errorf("Failed to read file: %v", err)
}
if err := tail.Close(); err != nil {
t.Errorf("Failed to close file: %v", err)
}
if buf.String() != "1\n2\n3\n" {
t.Errorf("Failed to read file, got %q, want %q", buf.String(), "1\n2\n3\n")
}
close(doneChan)
}()

Expand All @@ -68,8 +62,34 @@ func TestTail(t *testing.T) {
tail.Stop()

<-doneChan
if err := tail.Close(); err != nil {
t.Errorf("Failed to close file: %v", err)
}
finalInode := tail.Inode()

// There is a small problem here: tail.Stop() immediately stops reading from
// the inotify stream. There is no easy way to synchronize both events
// without poking into the implementation details of Tail.
//
// It is completely valid to have the contents of the stream be "1\n",
// "1\n2\n", and "1\n\2\n3\n". In the two versions, there is no guarantee
// about what the value of the final inode should be, since those events
// could feasibly appear in the stream after calling `tail.Stop()`.
if buf.String() == "1\n" {
t.Logf("Very short version detected, inode should still be the old one")
if finalInode != originalInode {
t.Errorf("Failed to detect the new inode, got %v, want %v", finalInode, originalInode)
}
return
}
if buf.String() == "1\n2\n" {
t.Logf("Short version detected, no guarantee about the inode")
return
}

if buf.String() != "1\n2\n3\n" {
t.Errorf("Failed to read file, got %q, want %q", buf.String(), "1\n2\n3\n")
}
if finalInode == originalInode {
t.Errorf("Failed to read new inode, got %v, want !%v", finalInode, originalInode)
}
Expand Down

0 comments on commit 76ea01a

Please sign in to comment.