MCPcopy Create free account
hub / github.com/erpc/erpc / TestMemoryConnector_ConcurrentCompression

Function TestMemoryConnector_ConcurrentCompression

data/memory_compression_test.go:140–244  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

138}
139
140func TestMemoryConnector_ConcurrentCompression(t *testing.T) {
141 // Test concurrent access to verify thread safety
142 logger := zerolog.New(io.Discard)
143 ctx := context.Background()
144
145 connector, err := NewMemoryConnector(ctx, &logger, "test", &common.MemoryConnectorConfig{
146 MaxItems: 100_000, MaxTotalSize: "1GB",
147 })
148 require.NoError(t, err)
149 defer connector.Close()
150
151 // Create test data of various sizes
152 testData := []struct {
153 key string
154 value string
155 }{
156 {"small1", `{"jsonrpc":"2.0","result":"0x123","id":1}`},
157 {"large1", createLargeEvmResponse()},
158 {"medium1", createMediumEvmResponse()},
159 {"large2", createLargeBlockResponse()},
160 }
161
162 // Reduced concurrency to account for Ristretto's async nature
163 numWorkers := 10
164 numOperationsPerWorker := 5
165
166 var wg sync.WaitGroup
167 errChan := make(chan error, numWorkers*numOperationsPerWorker*2) // *2 for Set+Get operations
168
169 // Phase 1: Concurrent writes
170 t.Log("Phase 1: Concurrent writes...")
171 for i := 0; i < numWorkers; i++ {
172 wg.Add(1)
173 go func(workerID int) {
174 defer wg.Done()
175
176 for j := 0; j < numOperationsPerWorker; j++ {
177 for _, testCase := range testData {
178 // Unique key per worker and operation
179 key := fmt.Sprintf("%s_w%d_op%d", testCase.key, workerID, j)
180
181 // Set operation
182 if err := connector.Set(ctx, "concurrent", key, []byte(testCase.value), nil); err != nil {
183 errChan <- fmt.Errorf("worker %d operation %d set failed: %w", workerID, j, err)
184 }
185
186 // Small delay between operations
187 time.Sleep(2 * time.Millisecond)
188 }
189 }
190 }(i)
191 }
192
193 // Wait for all writes to complete
194 wg.Wait()
195
196 // Allow Ristretto to process all buffered writes
197 t.Log("Waiting for Ristretto to process writes...")

Callers

nothing calls this directly

Calls 10

CloseMethod · 0.95
SetMethod · 0.95
GetMethod · 0.95
NewMemoryConnectorFunction · 0.85
createLargeEvmResponseFunction · 0.85
createMediumEvmResponseFunction · 0.85
createLargeBlockResponseFunction · 0.85
ErrorfMethod · 0.80
WaitMethod · 0.80
AddMethod · 0.65

Tested by

no test coverage detected