From af9638cb2d997f6cd9dce03f15c956a439e66947 Mon Sep 17 00:00:00 2001 From: instabledesign Date: Fri, 21 Feb 2020 18:32:08 +0100 Subject: [PATCH] Fix testing in memory logger race condition (#70) * Fix testing in memory logger race condition * add test race condition --- testing/logger.go | 6 ++++-- testing/logger_test.go | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/testing/logger.go b/testing/logger.go index 8499be7..fcce46d 100644 --- a/testing/logger.go +++ b/testing/logger.go @@ -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 } diff --git a/testing/logger_test.go b/testing/logger_test.go index f822780..fc96f15 100644 --- a/testing/logger_test.go +++ b/testing/logger_test.go @@ -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()