| 188 | } |
| 189 | |
| 190 | func TestWriteBatch_Close(t *testing.T) { |
| 191 | tempDir := t.TempDir() |
| 192 | cfg := &config.Config{ |
| 193 | LevelDB: config.LevelDBConfig{ |
| 194 | CacheSize: 64 * 1024 * 1024, |
| 195 | BlockSize: 4 * 1024, |
| 196 | WriteBufferSize: 4 * 1024 * 1024, |
| 197 | MaxOpenFiles: 1000, |
| 198 | Compression: true, |
| 199 | }, |
| 200 | } |
| 201 | |
| 202 | db, err := Store{}.Open(tempDir, cfg) |
| 203 | require.NoError(t, err) |
| 204 | defer db.Close() |
| 205 | |
| 206 | wb := db.NewWriteBatch() |
| 207 | |
| 208 | // Add some operations |
| 209 | wb.Put([]byte("key1"), []byte("value1")) |
| 210 | wb.Put([]byte("key2"), []byte("value2")) |
| 211 | |
| 212 | // Close the batch |
| 213 | wb.Close() |
| 214 | |
| 215 | // After closing, operations should not panic |
| 216 | func() { |
| 217 | defer func() { |
| 218 | if r := recover(); r != nil { |
| 219 | t.Fatalf("WriteBatch operations panicked after close: %v", r) |
| 220 | } |
| 221 | }() |
| 222 | |
| 223 | wb.Put([]byte("key3"), []byte("value3")) |
| 224 | wb.Delete([]byte("key1")) |
| 225 | _ = wb.Commit() |
| 226 | _ = wb.SyncCommit() |
| 227 | _ = wb.Rollback() |
| 228 | _ = wb.Data() |
| 229 | }() |
| 230 | |
| 231 | // Values should not be written since batch was closed before commit |
| 232 | val, err := db.Get([]byte("key1")) |
| 233 | require.NoError(t, err) |
| 234 | assert.Nil(t, val) |
| 235 | } |
| 236 | |
| 237 | func TestWriteBatch_Data(t *testing.T) { |
| 238 | tempDir := t.TempDir() |