From 8ada4aa3ce0fb522510371f711a09f0b9c0aa674 Mon Sep 17 00:00:00 2001 From: Ritchie6 <191115765+Ritchie6@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:45:20 +0530 Subject: [PATCH 1/2] Implemented the Close() method --- memoria.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/memoria.go b/memoria.go index 91ff36d..2539030 100644 --- a/memoria.go +++ b/memoria.go @@ -436,3 +436,18 @@ func (m *Memoria) BulkWrite(pairs map[string][]byte, numWorkers int) []WriteResu return results } + +// Implementing the Close() method: + +func (m *Memoria) Close() error { // Here, the Close() method only clears the in-memory cache + m.mu.Lock() + defer m.mu.Unlock() + + // Clearing the cache within the memory: + for key := range m.cache { + delete(m.cache, key) // To delete the key from cache map + } + m.cacheSize = 0 + + return nil +} From b7936d9e14a86ad575e2fb8e8ba62aa918038d10 Mon Sep 17 00:00:00 2001 From: Ritchie6 <191115765+Ritchie6@users.noreply.github.com> Date: Fri, 10 Jan 2025 03:06:46 +0530 Subject: [PATCH 2/2] Added tests for BulkWrite method --- test/memoria_test.go | 121 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/test/memoria_test.go b/test/memoria_test.go index 911fb58..cb53069 100644 --- a/test/memoria_test.go +++ b/test/memoria_test.go @@ -154,3 +154,124 @@ func TestMemoriaWriteReadString(t1 *testing.T) { }) } } + +func TestMemoriaBulkWrite(t *testing.T) { + + // temp-dir for tests + tempDir, err := os.MkdirTemp("", "memoria-test-*") + if err != nil { + t.Fatalf("Failed to create temp dir: %v", err) + } + defer os.RemoveAll(tempDir) + + m := memoria.New(memoria.Options{ + Basedir: tempDir, + MaxCacheSize: 1024, + }) + + tests := []struct { + name string + pairs map[string][]byte + numWorkers int + expectedErr bool + }{ + { + name: "Successful bulk write with multiple pairs", + pairs: map[string][]byte{ + "key1": []byte("value1"), + "key2": []byte("value2"), + "key3": []byte("value3"), + }, + numWorkers: 2, + expectedErr: false, + }, + { + name: "Bulk write with an empty key", + pairs: map[string][]byte{ + "": []byte("value1"), // Empty key should cause error + }, + numWorkers: 1, + expectedErr: true, + }, + { + name: "Bulk write with an empty value", + pairs: map[string][]byte{ + "key1": []byte{}, // Empty value should be handled correctly + }, + numWorkers: 1, + expectedErr: false, + }, + { + name: "Bulk write with large values", + pairs: map[string][]byte{ + "key1": bytes.Repeat([]byte("a"), 1000), // Large value + "key2": bytes.Repeat([]byte("b"), 1000), // Large value + }, + numWorkers: 2, + expectedErr: false, + }, + { + name: "Bulk write with mix of valid and invalid keys", + pairs: map[string][]byte{ + "validKey1": []byte("value1"), + "": []byte("value2"), // invalid key + }, + numWorkers: 2, + expectedErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + results := m.BulkWrite(tt.pairs, tt.numWorkers) + + // Check the number of results matches the number of pairs + if len(results) != len(tt.pairs) { + t.Errorf("Expected %d results, but got %d", len(tt.pairs), len(results)) + } + + var hasError bool + for _, result := range results { + if result.Error != nil { + hasError = true + } + } + + // Check if the results contain errors where expected + // for _, result := range results { + // if tt.expectedErr && result.Error == nil { + // t.Errorf("Expected error but got nil for key %s", result.Key) + // } + // if !tt.expectedErr && result.Error != nil { + // t.Errorf("Unexpected error for key %s: %v", result.Key, result.Error) + // } + // } + + // If we expect an error, ensure at least one result has an error + if tt.expectedErr && !hasError { + t.Errorf("Expected at least one error but got none") + } + + // If no error is expected, ensure all results are successful + if !tt.expectedErr && hasError { + t.Errorf("Unexpected error(s) occurred") + } + + // Verify the data was written correctly + for key, expectedValue := range tt.pairs { + if tt.expectedErr { + continue + } + got, err := m.Read(key) + if err != nil { + t.Errorf("Failed to read key %s: %v", key, err) + continue + } + if !bytes.Equal(got, expectedValue) { + t.Errorf("Read value for key %s = %v, want %v", key, got, expectedValue) + } + } + }) + } +}