mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-30 03:23:08 +03:00
use thread safe map
This commit is contained in:
59
pkg/utils/thread_safe_map_test.go
Normal file
59
pkg/utils/thread_safe_map_test.go
Normal file
@ -0,0 +1,59 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestThreadSafeMap(t *testing.T) {
|
||||
m := NewThreadSafeMap[int, int]()
|
||||
|
||||
m.Set(1, 1)
|
||||
m.Set(2, 2)
|
||||
m.Set(3, 3)
|
||||
|
||||
if m.Len() != 3 {
|
||||
t.Errorf("Expected length to be 3, got %d", m.Len())
|
||||
}
|
||||
|
||||
if !m.Has(1) {
|
||||
t.Errorf("Expected to have key 1")
|
||||
}
|
||||
|
||||
if m.Has(4) {
|
||||
t.Errorf("Expected to not have key 4")
|
||||
}
|
||||
|
||||
if _, ok := m.Get(1); !ok {
|
||||
t.Errorf("Expected to have key 1")
|
||||
}
|
||||
|
||||
if _, ok := m.Get(4); ok {
|
||||
t.Errorf("Expected to not have key 4")
|
||||
}
|
||||
|
||||
m.Delete(1)
|
||||
|
||||
if m.Has(1) {
|
||||
t.Errorf("Expected to not have key 1")
|
||||
}
|
||||
|
||||
m.Clear()
|
||||
|
||||
if m.Len() != 0 {
|
||||
t.Errorf("Expected length to be 0, got %d", m.Len())
|
||||
}
|
||||
}
|
||||
|
||||
func TestThreadSafeMapConcurrentReadWrite(t *testing.T) {
|
||||
m := NewThreadSafeMap[int, int]()
|
||||
|
||||
go func() {
|
||||
for i := 0; i < 10000; i++ {
|
||||
m.Set(0, 0)
|
||||
}
|
||||
}()
|
||||
|
||||
for i := 0; i < 10000; i++ {
|
||||
m.Get(0)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user