From 1e4b943789aeaec318e916c68fb6012e575e1f11 Mon Sep 17 00:00:00 2001 From: Joakim Olsson Date: Thu, 16 Jun 2022 11:52:48 +0200 Subject: [PATCH] fix: add mutex to fix race conditions for consumers --- LICENSE | 2 +- go.mod | 2 +- go.sum | 5 ----- mocks.go | 24 +++++++++++++----------- mocks_test.go | 19 ++++++++----------- 5 files changed, 23 insertions(+), 29 deletions(-) diff --git a/LICENSE b/LICENSE index 1501635..6fa518c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020 Unbound Software Development Svenska AB +Copyright (c) 2022 Unbound Software Development Svenska AB Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/go.mod b/go.mod index 57127be..822a968 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.15 require ( github.com/apex/log v1.9.0 - github.com/sanity-io/litter v1.5.5 + github.com/stretchr/testify v1.6.1 ) diff --git a/go.sum b/go.sum index c2211f2..8961318 100644 --- a/go.sum +++ b/go.sum @@ -5,7 +5,6 @@ github.com/aphistic/golf v0.0.0-20180712155816-02c07f170c5a/go.mod h1:3NqKYiepwy github.com/aphistic/sweet v0.2.0/go.mod h1:fWDlIh/isSE9n6EPsRmC0det+whmX6dJid3stzu0Xys= github.com/aws/aws-sdk-go v1.20.6/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I= -github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -33,18 +32,14 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/fastuuid v1.1.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= -github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQo= -github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/smartystreets/assertions v1.0.0/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM= github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h1:SnhjPscd9TpLiy1LpzGSKh3bXCfxxXuqd9xmQJy3slM= github.com/smartystreets/gunit v1.0.0/go.mod h1:qwPWnhz6pn0NnRBP++URONOVyNkPyr4SauJk4cUOwJs= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/mocks.go b/mocks.go index 3120f8d..028e0d0 100644 --- a/mocks.go +++ b/mocks.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020 Unbound Software Development Svenska AB +// Copyright (c) 2022 Unbound Software Development Svenska AB // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in @@ -21,19 +21,24 @@ package apex import ( "fmt" - "github.com/apex/log" - "github.com/sanity-io/litter" + "sync" "testing" + + "github.com/apex/log" + "github.com/stretchr/testify/assert" ) // Mock has a Logger for use in unit-testing type Mock struct { + *sync.RWMutex Logger log.Interface Logged []string } // HandleLog stores the logged entries to be able to check them later func (m *Mock) HandleLog(entry *log.Entry) error { + m.Lock() + defer m.Unlock() m.Logged = append(m.Logged, fmt.Sprintf("%s: %s", entry.Level.String(), entry.Message)) return nil } @@ -43,7 +48,8 @@ var _ log.Handler = &Mock{} // New instantiates a new Mock and sets the log.Handler to it func New() *Mock { mock := &Mock{ - Logger: log.Log, + RWMutex: &sync.RWMutex{}, + Logger: log.Log, } log.SetHandler(mock) return mock @@ -52,11 +58,7 @@ func New() *Mock { // Check verifies that the application has logged the expected strings func (m *Mock) Check(t testing.TB, wantLogged []string) { t.Helper() - if len(m.Logged) != 0 || len(wantLogged) != 0 { - got := litter.Sdump(m.Logged) - want := litter.Sdump(wantLogged) - if got != want { - t.Errorf("Logger() got %s, want %s", got, want) - } - } + m.RLock() + defer m.RUnlock() + assert.Equal(t, wantLogged, m.Logged) } diff --git a/mocks_test.go b/mocks_test.go index c4c27e7..04d50e7 100644 --- a/mocks_test.go +++ b/mocks_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020 Unbound Software Development Svenska AB +// Copyright (c) 2022 Unbound Software Development Svenska AB // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in @@ -21,8 +21,10 @@ package apex import ( "fmt" - "reflect" + "sync" "testing" + + "github.com/stretchr/testify/assert" ) func TestMock_Check(t *testing.T) { @@ -48,23 +50,18 @@ func TestMock_Check(t *testing.T) { name: "different", fields: fields{Logged: []string{"same"}}, args: args{wantLogged: []string{"different"}}, - want: []string{`Logger() got []string{ - "same", -}, want []string{ - "different", -}`}, + want: []string{"\n\tError Trace:\tmocks.go:63\n\t \t\t\t\tmocks_test.go:63\n\tError: \tNot equal: \n\t \texpected: []string{\"different\"}\n\t \tactual : []string{\"same\"}\n\t \t\n\t \tDiff:\n\t \t--- Expected\n\t \t+++ Actual\n\t \t@@ -1,3 +1,3 @@\n\t \t ([]string) (len=1) {\n\t \t- (string) (len=9) \"different\"\n\t \t+ (string) (len=4) \"same\"\n\t \t }\n\tTest: \t\n"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { m := &Mock{ - Logged: tt.fields.Logged, + RWMutex: &sync.RWMutex{}, + Logged: tt.fields.Logged, } temp := &MockT{} m.Check(temp, tt.args.wantLogged) - if !reflect.DeepEqual(temp.errors, tt.want) { - t.Errorf("Check() got %+v, want %+v", temp.errors, tt.want) - } + assert.Equal(t, tt.want, temp.errors) }) } } -- 2.52.0