1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-04 10:34:55 +02:00
lazygit/pkg/utils/thread_safe_map_test.go
2022-10-02 20:57:44 -07:00

60 lines
866 B
Go

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)
}
}