Skip to content

Commit

Permalink
Add locking to data_iterator_test.go
Browse files Browse the repository at this point in the history
Fixes `fatal error: concurrent map read and map write` that can occur when listeners are called concurrently
  • Loading branch information
grodowski committed Dec 9, 2024
1 parent 25f3578 commit 1e4299a
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions test/go/data_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ package test

import (
"fmt"
"github.com/stretchr/testify/suite"
"sync"
"testing"

"github.com/Shopify/ghostferry"
"github.com/Shopify/ghostferry/testhelpers"
"github.com/stretchr/testify/suite"
)

type DataIteratorTestSuite struct {
*testhelpers.GhostferryUnitTestSuite

di *ghostferry.DataIterator
wg *sync.WaitGroup
tables []*ghostferry.TableSchema
receivedRows map[string][]ghostferry.RowData
di *ghostferry.DataIterator
wg *sync.WaitGroup
tables []*ghostferry.TableSchema
receivedRows map[string][]ghostferry.RowData
receivedRowsMutex sync.Mutex
}

func (this *DataIteratorTestSuite) SetupTest() {
Expand Down Expand Up @@ -61,6 +62,9 @@ func (this *DataIteratorTestSuite) SetupTest() {
this.receivedRows = make(map[string][]ghostferry.RowData, 0)

this.di.AddBatchListener(func(ev *ghostferry.RowBatch) error {
this.receivedRowsMutex.Lock()
defer this.receivedRowsMutex.Unlock()

this.receivedRows[ev.TableSchema().Name] = append(this.receivedRows[ev.TableSchema().Name], ev.Values()...)
return nil
})
Expand Down

0 comments on commit 1e4299a

Please sign in to comment.