Skip to content

Commit

Permalink
fix: race condition in keploy record
Browse files Browse the repository at this point in the history
  • Loading branch information
dxtym committed May 7, 2024
1 parent 4410963 commit a8308a9
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/service/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"sync"

"time"

Expand Down Expand Up @@ -66,13 +67,18 @@ func (r *Recorder) Start(ctx context.Context) error {
var appID uint64
var newTestSetID string
var testCount = 0
var mutex = &sync.Mutex{}
var waitGroup = &sync.WaitGroup{}
var mockCountMap = make(map[string]int)

// defering the stop function to stop keploy in case of any error in record or in case of context cancellation
defer func() {
select {
case <-ctx.Done():
waitGroup.Wait()
mutex.Lock()
r.telemetry.RecordedTestSuite(newTestSetID, testCount, mockCountMap)
mutex.Unlock()
default:
err := utils.Stop(r.logger, stopReason)
if err != nil {
Expand Down Expand Up @@ -144,6 +150,7 @@ func (r *Recorder) Start(ctx context.Context) error {
return fmt.Errorf(stopReason)
}

waitGroup.Add(1)
errGrp.Go(func() error {
for testCase := range incomingChan {
err := r.testDB.InsertTestCase(ctx, testCase, newTestSetID)
Expand All @@ -153,8 +160,10 @@ func (r *Recorder) Start(ctx context.Context) error {
}
insertTestErrChan <- err
} else {

mutex.Lock()
testCount++
mutex.Unlock()
waitGroup.Done()
r.telemetry.RecordedTestAndMocks()
}
}
Expand Down

0 comments on commit a8308a9

Please sign in to comment.