mirror of
https://github.com/sot-tech/mochi.git
synced 2026-06-30 06:02:07 -07:00
store: use stopper, extraxt StringStore tests
This commit is contained in:
@@ -19,11 +19,13 @@ type stringStoreDriver struct{}
|
||||
func (d *stringStoreDriver) New(_ *store.DriverConfig) (store.StringStore, error) {
|
||||
return &stringStore{
|
||||
strings: make(map[string]struct{}),
|
||||
closed: make(chan struct{}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type stringStore struct {
|
||||
strings map[string]struct{}
|
||||
closed chan struct{}
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
@@ -33,6 +35,12 @@ func (ss *stringStore) PutString(s string) error {
|
||||
ss.Lock()
|
||||
defer ss.Unlock()
|
||||
|
||||
select {
|
||||
case <-ss.closed:
|
||||
panic("attempted to interact with stopped store")
|
||||
default:
|
||||
}
|
||||
|
||||
ss.strings[s] = struct{}{}
|
||||
|
||||
return nil
|
||||
@@ -42,6 +50,12 @@ func (ss *stringStore) HasString(s string) (bool, error) {
|
||||
ss.RLock()
|
||||
defer ss.RUnlock()
|
||||
|
||||
select {
|
||||
case <-ss.closed:
|
||||
panic("attempted to interact with stopped store")
|
||||
default:
|
||||
}
|
||||
|
||||
_, ok := ss.strings[s]
|
||||
|
||||
return ok, nil
|
||||
@@ -51,6 +65,12 @@ func (ss *stringStore) RemoveString(s string) error {
|
||||
ss.Lock()
|
||||
defer ss.Unlock()
|
||||
|
||||
select {
|
||||
case <-ss.closed:
|
||||
panic("attempted to interact with stopped store")
|
||||
default:
|
||||
}
|
||||
|
||||
if _, ok := ss.strings[s]; !ok {
|
||||
return store.ErrResourceDoesNotExist
|
||||
}
|
||||
@@ -59,3 +79,15 @@ func (ss *stringStore) RemoveString(s string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ss *stringStore) Stop() <-chan error {
|
||||
toReturn := make(chan error)
|
||||
go func() {
|
||||
ss.Lock()
|
||||
defer ss.Unlock()
|
||||
ss.strings = make(map[string]struct{})
|
||||
close(ss.closed)
|
||||
close(toReturn)
|
||||
}()
|
||||
return toReturn
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user