From e4891baad1d3149818b9dd043db85ab46827a435 Mon Sep 17 00:00:00 2001 From: Antoine Mercadal Date: Mon, 7 Oct 2019 16:00:26 -0700 Subject: [PATCH] fixed: test datarace --- maniptest/subscriber.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/maniptest/subscriber.go b/maniptest/subscriber.go index 9b4b3484..078226a9 100644 --- a/maniptest/subscriber.go +++ b/maniptest/subscriber.go @@ -55,31 +55,49 @@ func NewTestSubscriber() TestSubscriber { func (m *testSubscriber) MockStart(t *testing.T, impl func(context.Context, *elemental.PushFilter)) { + m.lock.Lock() + defer m.lock.Unlock() + m.currentMocks(t).startMock = impl } func (m *testSubscriber) MockUpdateFilter(t *testing.T, impl func(*elemental.PushFilter)) { + m.lock.Lock() + defer m.lock.Unlock() + m.currentMocks(t).updateFilerMock = impl } func (m *testSubscriber) MockEvents(t *testing.T, impl func() chan *elemental.Event) { + m.lock.Lock() + defer m.lock.Unlock() + m.currentMocks(t).eventsMock = impl } func (m *testSubscriber) MockErrors(t *testing.T, impl func() chan error) { + m.lock.Lock() + defer m.lock.Unlock() + m.currentMocks(t).errorsMock = impl } func (m *testSubscriber) MockStatus(t *testing.T, impl func() chan manipulate.SubscriberStatus) { + m.lock.Lock() + defer m.lock.Unlock() + m.currentMocks(t).statusMock = impl } func (m *testSubscriber) Start(ctx context.Context, filter *elemental.PushFilter) { + m.lock.Lock() + defer m.lock.Unlock() + if mock := m.currentMocks(m.currentTest); mock != nil && mock.startMock != nil { mock.startMock(ctx, filter) } @@ -88,6 +106,9 @@ func (m *testSubscriber) Start(ctx context.Context, filter *elemental.PushFilter func (m *testSubscriber) UpdateFilter(filter *elemental.PushFilter) { + m.lock.Lock() + defer m.lock.Unlock() + if mock := m.currentMocks(m.currentTest); mock != nil && mock.updateFilerMock != nil { mock.updateFilerMock(filter) } @@ -96,6 +117,9 @@ func (m *testSubscriber) UpdateFilter(filter *elemental.PushFilter) { func (m *testSubscriber) Events() chan *elemental.Event { + m.lock.Lock() + defer m.lock.Unlock() + if mock := m.currentMocks(m.currentTest); mock != nil && mock.eventsMock != nil { return mock.eventsMock() } @@ -105,6 +129,9 @@ func (m *testSubscriber) Events() chan *elemental.Event { func (m *testSubscriber) Errors() chan error { + m.lock.Lock() + defer m.lock.Unlock() + if mock := m.currentMocks(m.currentTest); mock != nil && mock.errorsMock != nil { return mock.errorsMock() } @@ -114,6 +141,9 @@ func (m *testSubscriber) Errors() chan error { func (m *testSubscriber) Status() chan manipulate.SubscriberStatus { + m.lock.Lock() + defer m.lock.Unlock() + if mock := m.currentMocks(m.currentTest); mock != nil && mock.statusMock != nil { return mock.statusMock() } @@ -123,9 +153,6 @@ func (m *testSubscriber) Status() chan manipulate.SubscriberStatus { func (m *testSubscriber) currentMocks(t *testing.T) *mockedSubscriberMethods { - m.lock.Lock() - defer m.lock.Unlock() - mocks := m.mocks[t] if mocks == nil {