-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make flumetest.Start() concurrency safe
- Loading branch information
Showing
3 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,78 @@ | ||
package flumetest | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gemalto/flume" | ||
"github.com/stretchr/testify/assert" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func init() { | ||
MustSetDefaults() | ||
} | ||
|
||
func TestStart(t *testing.T) { | ||
defer Start(t)() | ||
type mockT struct { | ||
failed bool | ||
lastLog string | ||
sync.Mutex | ||
} | ||
|
||
func (m *mockT) Failed() bool { | ||
m.Lock() | ||
defer m.Unlock() | ||
return m.failed | ||
} | ||
|
||
func (m *mockT) Log(args ...interface{}) { | ||
m.Lock() | ||
defer m.Unlock() | ||
m.lastLog = fmt.Sprint(args...) | ||
} | ||
|
||
func TestStart(t *testing.T) { | ||
var log = flume.New("TestStart") | ||
log.Info("Hi", "color", "red", "size", 5, "multilinevalue") | ||
|
||
fakeTestRun := func(succeed bool) string { | ||
m := mockT{ | ||
failed: !succeed, | ||
} | ||
finish := start(&m) | ||
|
||
log.Info("Hi", "color", "red") | ||
|
||
finish() | ||
m.Lock() | ||
defer m.Unlock() | ||
return m.lastLog | ||
} | ||
assert.Empty(t, fakeTestRun(true), "should not have logged, because the test didn't fail") | ||
assert.Contains(t, fakeTestRun(false), "color:red", "should have logged since test failed") | ||
|
||
// this test is meant to trigger the race detector if we're not synchronizing correctly on the message | ||
// buffer | ||
m := mockT{ | ||
failed: true, | ||
} | ||
finish := start(&m) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
stop := make(chan struct{}) | ||
go func() { | ||
wg.Done() | ||
for { | ||
select { | ||
case <-stop: | ||
return | ||
default: | ||
log.Info("logging on a different goroutine, for race detector") | ||
} | ||
} | ||
|
||
}() | ||
wg.Wait() | ||
finish() | ||
stop <- struct{}{} | ||
|
||
} |