github.com/aaabigfish/gopkg@v1.1.0/cache/lrucache/synccache_test.go (about) 1 package lrucache 2 3 import ( 4 "sync" 5 "testing" 6 "time" 7 ) 8 9 func Test_hashCode(t *testing.T) { 10 /*if hashCode(-1) != 1 { 11 t.Error("case 1 failed") 12 } 13 if hashCode(0) != 0 { 14 t.Error("case 2 failed") 15 } 16 if hashCode(0x7FFFFFFF) != 0x7FFFFFFF { 17 t.Error("case 3 failed") 18 }*/ 19 if hashCode("12345") != 3421846044 { 20 t.Error("case 4 failed") 21 } 22 if hashCode("abcdefghijklmnopqrstuvwxyz") != 1277644989 { 23 t.Error("case 5 failed") 24 } 25 /*if hashCode(123.45) != 123 { 26 t.Error("case 6 failed") 27 } 28 if hashCode(-15268.45) != 15268 { 29 t.Error("case 7 failed") 30 }*/ 31 } 32 33 func Test_nextPowOf2(t *testing.T) { 34 if nextPowOf2(0) != 2 { 35 t.Error("case 1 failed") 36 } 37 if nextPowOf2(1) != 2 { 38 t.Error("case 2 failed") 39 } 40 if nextPowOf2(2) != 2 { 41 t.Error("case 3 failed") 42 } 43 if nextPowOf2(3) != 4 { 44 t.Error("case 4 failed") 45 } 46 if nextPowOf2(123) != 128 { 47 t.Error("case 5 failed") 48 } 49 if nextPowOf2(0x7FFFFFFF) != 0x80000000 { 50 t.Error("case 6 failed") 51 } 52 } 53 54 func Test_timeout(t *testing.T) { 55 sc := NewSyncCache(1, 2, 2) 56 sc.Put("1", "2") 57 if v, ok := sc.Get("1"); !ok || v != "2" { 58 t.Error("case 1 failed") 59 } 60 time.Sleep(2 * time.Second) 61 if _, ok := sc.Get("1"); ok { 62 t.Error("case 2 failed") 63 } 64 } 65 66 func Test_concurrent(t *testing.T) { 67 sc := NewSyncCache(1, 4, 2) 68 var wg sync.WaitGroup 69 for index := 0; index < 100000; index++ { 70 wg.Add(3) 71 go func() { 72 sc.Put("1", "2") 73 wg.Done() 74 }() 75 go func() { 76 sc.Get("1") 77 wg.Done() 78 }() 79 go func() { 80 sc.Delete("1") 81 wg.Done() 82 }() 83 } 84 wg.Wait() 85 }