Skip to content

Commit

Permalink
Fix testing in memory logger race condition (#70)
Browse files Browse the repository at this point in the history
* Fix testing in memory logger race condition

* add test race condition
  • Loading branch information
instabledesign authored Feb 21, 2020
1 parent dd255c1 commit af9638c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions testing/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ func (s *inMemoryEntriesStore) CleanEntries() {

// GetEntries will return the in memory entries list
func (s *inMemoryEntriesStore) GetEntries() []logger.Entry {
defer s.lock()()
return s.entries
}

// GetAndCleanEntries will return and clean the in memory entries list
func (s *inMemoryEntriesStore) GetAndCleanEntries() []logger.Entry {
entries := s.GetEntries()
s.CleanEntries()
defer s.lock()()
entries := s.entries
s.entries = []logger.Entry{}
return entries
}

Expand Down
12 changes: 12 additions & 0 deletions testing/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ func TestNewLogger(t *testing.T) {
assert.Equal(t, "test Info", entry2.Message)
}

func TestNewLoggerRace(t *testing.T) {
myLogger, store := testing_logger.NewLogger()

go func() {
myLogger.Debug("test Debug")
}()

go func() {
store.GetEntries()
}()
}

func TestNewLogger_GetEntries(t *testing.T) {
myLogger, store := testing_logger.NewLogger()

Expand Down

0 comments on commit af9638c

Please sign in to comment.