implement set for filestore
This commit is contained in:
parent
b6c03fe1a2
commit
382a28f775
@ -47,7 +47,9 @@ func (m *MockStore) Init() error {
|
|||||||
return args.Error(0)
|
return args.Error(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockStore) Set(services map[string][]string) {
|
func (m *MockStore) Set(services map[string][]string) error {
|
||||||
|
args := m.Called()
|
||||||
|
return args.Error(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContains(t *testing.T) {
|
func TestContains(t *testing.T) {
|
||||||
|
@ -20,7 +20,6 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -46,7 +45,7 @@ type Store interface {
|
|||||||
Init() error
|
Init() error
|
||||||
|
|
||||||
// Set stores the services information into internal state
|
// Set stores the services information into internal state
|
||||||
Set(services map[string][]string)
|
Set(services map[string][]string) error
|
||||||
|
|
||||||
// Remove the service from the store by address
|
// Remove the service from the store by address
|
||||||
Remove(addr string) error
|
Remove(addr string) error
|
||||||
@ -111,34 +110,7 @@ func (l *fileStore) Remove(addr string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := ioutil.ReadFile(l.persistentFile)
|
return l.Set(l.memoryStore.GetAll())
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to open file, path: %s, err: %v", l.persistentFile, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
newServices := ""
|
|
||||||
for _, line := range strings.Split(string(content), "\n") {
|
|
||||||
// addr(ip:port) is unique, so no need to check name
|
|
||||||
if strings.Contains(line, addr) {
|
|
||||||
// if the service match the string in the store, skip
|
|
||||||
} else {
|
|
||||||
newServices += line + "\n"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// write back to file
|
|
||||||
f, err := os.OpenFile(l.persistentFile, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0600)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to open file, path: %s, err: %v", l.persistentFile, err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
_, err = f.WriteString(newServices)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return f.Sync()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init cleanup all the registered service information
|
// Init cleanup all the registered service information
|
||||||
@ -195,8 +167,32 @@ func (l *fileStore) load() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *fileStore) Set(services map[string][]string) {
|
func (l *fileStore) Set(services map[string][]string) error {
|
||||||
panic("TO BE IMPLEMENTED")
|
l.mu.Lock()
|
||||||
|
defer l.mu.Unlock()
|
||||||
|
|
||||||
|
// no error will return from memorystore.set
|
||||||
|
_ = l.memoryStore.Set(services)
|
||||||
|
|
||||||
|
f, err := os.OpenFile(l.persistentFile, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0600)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
s := ""
|
||||||
|
for name, addrs := range services {
|
||||||
|
for _, addr := range addrs {
|
||||||
|
s += fmt.Sprintf("%s&%s\n", name, addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = f.WriteString(s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = f.Sync()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *fileStore) appendToFile(s ServiceUnderTest) error {
|
func (l *fileStore) appendToFile(s ServiceUnderTest) error {
|
||||||
@ -281,11 +277,13 @@ func (l *memoryStore) Init() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *memoryStore) Set(services map[string][]string) {
|
func (l *memoryStore) Set(services map[string][]string) error {
|
||||||
l.mu.Lock()
|
l.mu.Lock()
|
||||||
defer l.mu.Unlock()
|
defer l.mu.Unlock()
|
||||||
|
|
||||||
l.servicesMap = services
|
l.servicesMap = services
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove one service from the memory store
|
// Remove one service from the memory store
|
||||||
|
Loading…
Reference in New Issue
Block a user