mirror of
https://github.com/turbot/steampipe.git
synced 2026-03-21 07:00:11 -04:00
* Add test for #4716: sort() should be safe for concurrent calls * Fix #4716: Add mutex protection to autoCompleteSuggestions.sort() Adds sync.RWMutex to prevent data race during concurrent sort() calls. Changes sort() from value receiver to pointer receiver to support locking. The mutex ensures thread-safe access when multiple goroutines call sort() simultaneously during autocomplete initialization.
65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package interactive
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/c-bata/go-prompt"
|
|
)
|
|
|
|
// TestAutoCompleteSuggestions_ConcurrentSort tests that sort() can be called
|
|
// concurrently without triggering data races.
|
|
// This test reproduces the race condition reported in issue #4716.
|
|
func TestAutoCompleteSuggestions_ConcurrentSort(t *testing.T) {
|
|
// Create a populated autoCompleteSuggestions instance
|
|
suggestions := newAutocompleteSuggestions()
|
|
|
|
// Populate with test data
|
|
suggestions.schemas = []prompt.Suggest{
|
|
{Text: "public"},
|
|
{Text: "aws"},
|
|
{Text: "github"},
|
|
}
|
|
|
|
suggestions.unqualifiedTables = []prompt.Suggest{
|
|
{Text: "table1"},
|
|
{Text: "table2"},
|
|
{Text: "table3"},
|
|
}
|
|
|
|
suggestions.unqualifiedQueries = []prompt.Suggest{
|
|
{Text: "query1"},
|
|
{Text: "query2"},
|
|
{Text: "query3"},
|
|
}
|
|
|
|
suggestions.tablesBySchema["public"] = []prompt.Suggest{
|
|
{Text: "users"},
|
|
{Text: "accounts"},
|
|
}
|
|
|
|
suggestions.queriesByMod["aws"] = []prompt.Suggest{
|
|
{Text: "aws_query1"},
|
|
{Text: "aws_query2"},
|
|
}
|
|
|
|
// Call sort() concurrently from multiple goroutines
|
|
// This should trigger a race condition if the sort() method is not thread-safe
|
|
var wg sync.WaitGroup
|
|
numGoroutines := 10
|
|
|
|
for i := 0; i < numGoroutines; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
suggestions.sort()
|
|
}()
|
|
}
|
|
|
|
// Wait for all goroutines to complete
|
|
wg.Wait()
|
|
|
|
// If we get here without panicking or race detector errors, the test passes
|
|
// Note: This test will fail when run with -race flag if sort() is not thread-safe
|
|
}
|