Skip to content

Commit

Permalink
[chore][pkg/stanaza] Fix and strengthen test case (open-telemetry#28228)
Browse files Browse the repository at this point in the history
Follows:
open-telemetry#28206

This PR fixes a setup problem with a particular test and further
enhances it to make it more robust.

The problem was that it restarts a single operator, which is not
strictly supported behavior. Instead, a new operator should be created
from the same config.

The test involves moving a file and then validating that a log written
to the original file location is read correctly. This enhances the test
to also validate that additional logs written to the original (moved)
file will be read as well.
  • Loading branch information
djaglowski committed Oct 23, 2023
1 parent 93cde30 commit ac5407c
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions pkg/stanza/fileconsumer/rotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,31 +633,33 @@ func TestFileMovedWhileOff_BigFiles(t *testing.T) {
operator, emitCalls := buildTestManager(t, cfg)
persister := testutil.NewUnscopedMockPersister()

log1 := tokenWithLength(1000)
log2 := tokenWithLength(1000)
log1 := tokenWithLength(1001)
log2 := tokenWithLength(1002)
log3 := tokenWithLength(1003)

temp := openTemp(t, tempDir)
tempName := temp.Name()
writeString(t, temp, string(log1)+"\n")
require.NoError(t, temp.Close())

// Start the operator
// Run the operator to read the first log
require.NoError(t, operator.Start(persister))
defer func() {
require.NoError(t, operator.Stop())
}()
waitForToken(t, emitCalls, log1)

// Stop the operator, then rename and write a new log
require.NoError(t, operator.Stop())

err := os.Rename(temp.Name(), fmt.Sprintf("%s2", temp.Name()))
require.NoError(t, err)

temp = reopenTemp(t, temp.Name())
require.NoError(t, err)
// Write one more log to the original file
writeString(t, temp, string(log2)+"\n")
require.NoError(t, temp.Close())

// Rename the file and open another file in the same location
require.NoError(t, os.Rename(tempName, fmt.Sprintf("%s2", tempName)))

// Write a different log to the new file
temp2 := reopenTemp(t, tempName)
writeString(t, temp2, string(log3)+"\n")

// Expect the message written to the new log to come through
require.NoError(t, operator.Start(persister))
waitForToken(t, emitCalls, log2)
operator2, emitCalls2 := buildTestManager(t, cfg)
require.NoError(t, operator2.Start(persister))
waitForTokens(t, emitCalls2, log2, log3)
require.NoError(t, operator2.Stop())
}

0 comments on commit ac5407c

Please sign in to comment.