From b0cac278d23db0769ade22b163084f6ff0285cc8 Mon Sep 17 00:00:00 2001 From: ykomissarov Date: Thu, 9 Jan 2020 18:06:50 +0300 Subject: [PATCH] add mutex --- accounting/accounting_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/accounting/accounting_test.go b/accounting/accounting_test.go index 4689ad8..543c78c 100644 --- a/accounting/accounting_test.go +++ b/accounting/accounting_test.go @@ -151,6 +151,7 @@ func TestWorker_recoveryAccounting(t *testing.T) { type snapshotRepository struct { enableWaitGroup bool wg sync.WaitGroup + mx sync.Mutex cache map[int32]entity.Snapshot } @@ -177,6 +178,9 @@ func (r *snapshotRepository) Wait() bool { } func (r *snapshotRepository) GetByApplication(appId int32) (*entity.Snapshot, error) { + r.mx.Lock() + defer r.mx.Unlock() + if snapshot, ok := r.cache[appId]; ok { return &snapshot, nil } else { @@ -185,6 +189,9 @@ func (r *snapshotRepository) GetByApplication(appId int32) (*entity.Snapshot, er } func (r *snapshotRepository) Update(list []entity.Snapshot) error { + r.mx.Lock() + defer r.mx.Unlock() + if r.enableWaitGroup { r.wg.Add(1) defer r.wg.Done()