Fix race in destination unit test (#8065)

Fixes report https://github.com/linkerd/linkerd2/runs/5518386921
by guarding `BufferingProfileListener` with a mutex.
This commit is contained in:
Alejandro Pedraza 2022-03-14 13:26:05 -05:00 committed by GitHub
parent ce98206270
commit 30e42f98f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 0 deletions

View File

@ -94,6 +94,8 @@ func TestProfileWatcherUpdates(t *testing.T) {
actualProfiles := make([]*sp.ServiceProfileSpec, 0)
listener.mu.RLock()
defer listener.mu.RUnlock()
for _, profile := range listener.Profiles {
if profile == nil {
actualProfiles = append(actualProfiles, nil)

View File

@ -3,6 +3,7 @@ package watcher
import (
"encoding/json"
"reflect"
"sync"
"testing"
sp "github.com/linkerd/linkerd2/controller/gen/apis/serviceprofile/v1alpha2"
@ -32,6 +33,7 @@ func (dpl *DeletingProfileListener) Update(profile *sp.ServiceProfile) {
// in a slice. Useful for unit tests.
type BufferingProfileListener struct {
Profiles []*sp.ServiceProfile
mu sync.RWMutex
}
// NewBufferingProfileListener creates a new BufferingProfileListener.
@ -43,6 +45,8 @@ func NewBufferingProfileListener() *BufferingProfileListener {
// Update stores the update in the internal buffer.
func (bpl *BufferingProfileListener) Update(profile *sp.ServiceProfile) {
bpl.mu.Lock()
defer bpl.mu.Unlock()
bpl.Profiles = append(bpl.Profiles, profile)
}