fork of indigo with slightly nicer lexgen

Make MockDirectory Thread Safe

jcalabro 9db269ae edc81792

Changed files
+27
atproto
+27
atproto/identity/mock_directory.go
··· 4 "context" 5 "encoding/json" 6 "fmt" 7 8 "github.com/bluesky-social/indigo/atproto/syntax" 9 ) 10 11 // A fake identity directory, for use in tests 12 type MockDirectory struct { 13 Handles map[syntax.Handle]syntax.DID 14 Identities map[syntax.DID]Identity 15 } ··· 19 20 func NewMockDirectory() MockDirectory { 21 return MockDirectory{ 22 Handles: make(map[syntax.Handle]syntax.DID), 23 Identities: make(map[syntax.DID]Identity), 24 } 25 } 26 27 func (d *MockDirectory) Insert(ident Identity) { 28 if !ident.Handle.IsInvalidHandle() { 29 d.Handles[ident.Handle.Normalize()] = ident.DID 30 } ··· 32 } 33 34 func (d *MockDirectory) LookupHandle(ctx context.Context, h syntax.Handle) (*Identity, error) { 35 h = h.Normalize() 36 did, ok := d.Handles[h] 37 if !ok { ··· 45 } 46 47 func (d *MockDirectory) LookupDID(ctx context.Context, did syntax.DID) (*Identity, error) { 48 ident, ok := d.Identities[did] 49 if !ok { 50 return nil, ErrDIDNotFound ··· 53 } 54 55 func (d *MockDirectory) Lookup(ctx context.Context, a syntax.AtIdentifier) (*Identity, error) { 56 handle, err := a.AsHandle() 57 if nil == err { // if not an error, is a Handle 58 return d.LookupHandle(ctx, handle) ··· 65 } 66 67 func (d *MockDirectory) ResolveHandle(ctx context.Context, h syntax.Handle) (syntax.DID, error) { 68 h = h.Normalize() 69 did, ok := d.Handles[h] 70 if !ok { ··· 74 } 75 76 func (d *MockDirectory) ResolveDID(ctx context.Context, did syntax.DID) (*DIDDocument, error) { 77 ident, ok := d.Identities[did] 78 if !ok { 79 return nil, ErrDIDNotFound ··· 83 } 84 85 func (d *MockDirectory) ResolveDIDRaw(ctx context.Context, did syntax.DID) (json.RawMessage, error) { 86 ident, ok := d.Identities[did] 87 if !ok { 88 return nil, ErrDIDNotFound ··· 92 } 93 94 func (d *MockDirectory) Purge(ctx context.Context, a syntax.AtIdentifier) error { 95 return nil 96 }
··· 4 "context" 5 "encoding/json" 6 "fmt" 7 + "sync" 8 9 "github.com/bluesky-social/indigo/atproto/syntax" 10 ) 11 12 // A fake identity directory, for use in tests 13 type MockDirectory struct { 14 + mu *sync.RWMutex 15 Handles map[syntax.Handle]syntax.DID 16 Identities map[syntax.DID]Identity 17 } ··· 21 22 func NewMockDirectory() MockDirectory { 23 return MockDirectory{ 24 + mu: &sync.RWMutex{}, 25 Handles: make(map[syntax.Handle]syntax.DID), 26 Identities: make(map[syntax.DID]Identity), 27 } 28 } 29 30 func (d *MockDirectory) Insert(ident Identity) { 31 + d.mu.Lock() 32 + defer d.mu.Unlock() 33 + 34 if !ident.Handle.IsInvalidHandle() { 35 d.Handles[ident.Handle.Normalize()] = ident.DID 36 } ··· 38 } 39 40 func (d *MockDirectory) LookupHandle(ctx context.Context, h syntax.Handle) (*Identity, error) { 41 + d.mu.RLock() 42 + defer d.mu.RUnlock() 43 + 44 h = h.Normalize() 45 did, ok := d.Handles[h] 46 if !ok { ··· 54 } 55 56 func (d *MockDirectory) LookupDID(ctx context.Context, did syntax.DID) (*Identity, error) { 57 + d.mu.RLock() 58 + defer d.mu.RUnlock() 59 + 60 ident, ok := d.Identities[did] 61 if !ok { 62 return nil, ErrDIDNotFound ··· 65 } 66 67 func (d *MockDirectory) Lookup(ctx context.Context, a syntax.AtIdentifier) (*Identity, error) { 68 + d.mu.RLock() 69 + defer d.mu.RUnlock() 70 + 71 handle, err := a.AsHandle() 72 if nil == err { // if not an error, is a Handle 73 return d.LookupHandle(ctx, handle) ··· 80 } 81 82 func (d *MockDirectory) ResolveHandle(ctx context.Context, h syntax.Handle) (syntax.DID, error) { 83 + d.mu.RLock() 84 + defer d.mu.RUnlock() 85 + 86 h = h.Normalize() 87 did, ok := d.Handles[h] 88 if !ok { ··· 92 } 93 94 func (d *MockDirectory) ResolveDID(ctx context.Context, did syntax.DID) (*DIDDocument, error) { 95 + d.mu.RLock() 96 + defer d.mu.RUnlock() 97 + 98 ident, ok := d.Identities[did] 99 if !ok { 100 return nil, ErrDIDNotFound ··· 104 } 105 106 func (d *MockDirectory) ResolveDIDRaw(ctx context.Context, did syntax.DID) (json.RawMessage, error) { 107 + d.mu.RLock() 108 + defer d.mu.RUnlock() 109 + 110 ident, ok := d.Identities[did] 111 if !ok { 112 return nil, ErrDIDNotFound ··· 116 } 117 118 func (d *MockDirectory) Purge(ctx context.Context, a syntax.AtIdentifier) error { 119 + d.mu.Lock() 120 + defer d.mu.Unlock() 121 + 122 return nil 123 }