MCPcopy Create free account
hub / github.com/allegro/bigcache / TestAppendRandomly

Function TestAppendRandomly

bigcache_test.go:82–143  ·  view source on GitHub ↗

TestAppendRandomly does simultaneous appends to check for corruption errors.

(t *testing.T)

Source from the content-addressed store, hash-verified

80
81// TestAppendRandomly does simultaneous appends to check for corruption errors.
82func TestAppendRandomly(t *testing.T) {
83 t.Parallel()
84
85 c := Config{
86 Shards: 1,
87 LifeWindow: 5 * time.Second,
88 CleanWindow: 1 * time.Second,
89 MaxEntriesInWindow: 1000 * 10 * 60,
90 MaxEntrySize: 500,
91 StatsEnabled: true,
92 Verbose: true,
93 Hasher: newDefaultHasher(),
94 HardMaxCacheSize: 1,
95 Logger: DefaultLogger(),
96 }
97 cache, err := New(context.Background(), c)
98 noError(t, err)
99
100 nKeys := 5
101 nAppendsPerKey := 2000
102 nWorker := 10
103 var keys []string
104 for i := 0; i < nKeys; i++ {
105 for j := 0; j < nAppendsPerKey; j++ {
106 keys = append(keys, fmt.Sprintf("key%d", i))
107 }
108 }
109 rand.Shuffle(len(keys), func(i, j int) {
110 keys[i], keys[j] = keys[j], keys[i]
111 })
112
113 jobs := make(chan string, len(keys))
114 for _, key := range keys {
115 jobs <- key
116 }
117 close(jobs)
118
119 var wg sync.WaitGroup
120 for i := 0; i < nWorker; i++ {
121 wg.Add(1)
122 go func() {
123 for {
124 key, ok := <-jobs
125 if !ok {
126 break
127 }
128 cache.Append(key, []byte(key))
129 }
130 wg.Done()
131 }()
132 }
133 wg.Wait()
134
135 assertEqual(t, nKeys, cache.Len())
136 for i := 0; i < nKeys; i++ {
137 key := fmt.Sprintf("key%d", i)
138 expectedValue := []byte(strings.Repeat(key, nAppendsPerKey))
139 cachedValue, err := cache.Get(key)

Callers

nothing calls this directly

Calls 8

newDefaultHasherFunction · 0.85
DefaultLoggerFunction · 0.85
NewFunction · 0.85
AppendMethod · 0.80
noErrorFunction · 0.70
assertEqualFunction · 0.70
LenMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…