Skip to content

Commit 2bd8031

Browse files
committed
fix: implement document store isolation in tests
1 parent f938e23 commit 2bd8031

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

excalidraw-server/stores/memory/documents.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,46 @@ import (
44
"context"
55
"excalidraw-server/core"
66
"fmt"
7+
"sync"
78

89
"github.com/oklog/ulid/v2"
910
"github.com/sirupsen/logrus"
1011
)
1112

12-
var savedDocuments = make(map[string]core.Document)
13-
1413
type documentStore struct {
14+
mu sync.RWMutex
15+
documents map[string]core.Document
1516
}
1617

1718
func NewDocumentStore() core.DocumentStore {
18-
return &documentStore{}
19+
return &documentStore{
20+
documents: make(map[string]core.Document),
21+
}
1922
}
2023

2124
func (s *documentStore) FindID(ctx context.Context, id string) (*core.Document, error) {
2225
log := logrus.WithField("document_id", id)
23-
if val, ok := savedDocuments[id]; ok {
26+
27+
s.mu.RLock()
28+
doc, ok := s.documents[id]
29+
s.mu.RUnlock()
30+
31+
if ok {
2432
log.Info("Document retrieved successfully")
25-
return &val, nil
33+
return &doc, nil
2634
}
35+
2736
log.WithField("error", "document not found").Warn("Document with specified ID not found")
2837
return nil, fmt.Errorf("document with id %s not found", id)
2938
}
3039

3140
func (s *documentStore) Create(ctx context.Context, document *core.Document) (string, error) {
3241
id := ulid.Make().String()
33-
savedDocuments[id] = *document
42+
43+
s.mu.Lock()
44+
s.documents[id] = *document
45+
s.mu.Unlock()
46+
3447
log := logrus.WithFields(logrus.Fields{
3548
"document_id": id,
3649
"data_length": len(document.Data.Bytes()),

excalidraw-server/stores/memory/documents_test.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,24 @@ func TestContextCancellation(t *testing.T) {
351351
}
352352

353353
func TestStoreIsolation(t *testing.T) {
354-
// NOTE: The current in-memory implementation uses a global map,
355-
// so isolation between store instances is not supported.
356-
// This test documents the current behavior.
357-
// In a production system, you'd want each store instance to have its own map.
354+
ctx := context.Background()
355+
356+
store1 := NewDocumentStore()
357+
store2 := NewDocumentStore()
358+
359+
doc := &core.Document{Data: *bytes.NewBufferString("store1")}
360+
id, err := store1.Create(ctx, doc)
361+
if err != nil {
362+
t.Fatalf("Create() failed in store1: %v", err)
363+
}
364+
365+
// store1 should find the document
366+
if _, err := store1.FindID(ctx, id); err != nil {
367+
t.Fatalf("store1 should locate its document: %v", err)
368+
}
358369

359-
t.Skip("Skipping test - memory store uses global state (by design for this simple implementation)")
370+
// store2 should not see store1's document
371+
if _, err := store2.FindID(ctx, id); err == nil {
372+
t.Fatalf("store2 should not find document created by store1")
373+
}
360374
}

0 commit comments

Comments
 (0)