Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Tests for BulkWrite method #50

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions memoria.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
121 changes: 121 additions & 0 deletions test/memoria_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
})
}
}
Loading