Skip to content

Commit a625504

Browse files
author
razvan
committed
fix: resolve data races in test mocks under -race detector
1 parent c2229ca commit a625504

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

internal/service/engine/engine_exact_search_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package engine
33
import (
44
"context"
55
"errors"
6+
"sync"
67
"testing"
78

89
"github.com/doITmagic/rag-code-mcp/internal/config"
@@ -22,6 +23,7 @@ type testStore struct {
2223
storage.VectorStore
2324
existing map[string]bool
2425
exactSearchFunc func(coll string, filters map[string]interface{}) ([]storage.SearchResult, error)
26+
mu sync.Mutex
2527
recordedFilters []capturedCall
2628
exactSearchErr error
2729
}
@@ -36,7 +38,9 @@ return s.existing[name], nil
3638
}
3739

3840
func (s *testStore) ExactSearch(_ context.Context, coll string, filters map[string]interface{}, _ int) ([]storage.SearchResult, error) {
41+
s.mu.Lock()
3942
s.recordedFilters = append(s.recordedFilters, capturedCall{collection: coll, filters: filters})
43+
s.mu.Unlock()
4044
if s.exactSearchErr != nil {
4145
return nil, s.exactSearchErr
4246
}

internal/service/tools/tests/mocks_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tests
22

33
import (
44
"context"
5+
"sync"
56

67
"github.com/doITmagic/rag-code-mcp/internal/config"
78
"github.com/doITmagic/rag-code-mcp/internal/service/engine"
@@ -15,6 +16,7 @@ import (
1516

1617
type mockVectorStore struct {
1718
storage.VectorStore
19+
mu sync.Mutex
1820
SearchFunc func(ctx context.Context, collection string, query storage.SearchQuery) ([]storage.SearchResult, error)
1921
SearchCodeOnlyFunc func(ctx context.Context, collection string, query storage.SearchQuery) ([]storage.SearchResult, error)
2022
ExactSearchFunc func(ctx context.Context, collection string, filters map[string]interface{}, limit int) ([]storage.SearchResult, error)
@@ -37,6 +39,8 @@ func (m *mockVectorStore) SearchCodeOnly(ctx context.Context, collection string,
3739
}
3840

3941
func (m *mockVectorStore) ExactSearch(ctx context.Context, collection string, filters map[string]interface{}, limit int) ([]storage.SearchResult, error) {
42+
m.mu.Lock()
43+
defer m.mu.Unlock()
4044
if m.ExactSearchFunc != nil {
4145
return m.ExactSearchFunc(ctx, collection, filters, limit)
4246
}

0 commit comments

Comments
 (0)