slaskit
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 28s
Build and Publish / BuildAndDeploy (push) Successful in 2m23s

This commit is contained in:
matst80
2024-11-13 08:58:40 +01:00
parent c9a7113e12
commit 3615d2d7d1
4 changed files with 19 additions and 4 deletions

View File

@@ -41,8 +41,10 @@ func TestWatch(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("Error watching: %v", err) t.Errorf("Error watching: %v", err)
} }
select { select {
case <-ch: case m := <-ch:
t.Logf("Received watch %v", m)
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Errorf("Timeout waiting for watch") t.Errorf("Timeout waiting for watch")
} }

View File

@@ -244,6 +244,7 @@ func (p *SyncedPool) IsKnown(host string) bool {
return true return true
} }
} }
return host == p.Hostname return host == p.Hostname
} }
@@ -321,6 +322,7 @@ func (p *SyncedPool) Negotiate() {
func (p *SyncedPool) RequestOwnership(id CartId) error { func (p *SyncedPool) RequestOwnership(id CartId) error {
ok := 0 ok := 0
all := 0 all := 0
p.mu.RLock()
for _, r := range p.remotes { for _, r := range p.remotes {
log.Printf("Asking for confirmation change of %s to %s (me) with %s\n", id, p.Hostname, r.Host) log.Printf("Asking for confirmation change of %s to %s (me) with %s\n", id, p.Hostname, r.Host)
err := r.ConfirmChange(id, p.Hostname) err := r.ConfirmChange(id, p.Hostname)
@@ -336,6 +338,7 @@ func (p *SyncedPool) RequestOwnership(id CartId) error {
} }
ok++ ok++
} }
p.mu.RUnlock()
if ok == 0 && all > 0 { if ok == 0 && all > 0 {
p.removeLocalGrain(id) p.removeLocalGrain(id)
return fmt.Errorf("no remotes confirmed change") return fmt.Errorf("no remotes confirmed change")
@@ -354,6 +357,8 @@ func (p *SyncedPool) removeLocalGrain(id CartId) {
} }
func (p *SyncedPool) AddRemote(host string) error { func (p *SyncedPool) AddRemote(host string) error {
p.mu.Lock()
defer p.mu.Unlock()
_, hasHost := p.remotes[host] _, hasHost := p.remotes[host]
if host == "" || p.IsKnown(host) || hasHost { if host == "" || p.IsKnown(host) || hasHost {
return nil return nil

View File

@@ -16,12 +16,12 @@ func TestConnection(t *testing.T) {
TotalPrice: 0, TotalPrice: 0,
}, nil }, nil
}) })
pool, err := NewSyncedPool(localPool, "localhost", nil) pool, err := NewSyncedPool(localPool, "127.0.0.1", nil)
if err != nil { if err != nil {
t.Errorf("Error creating pool: %v", err) t.Errorf("Error creating pool: %v", err)
} }
err = pool.AddRemote("localhost") err = pool.AddRemote("127.0.0.1")
if err != nil { if err != nil {
t.Errorf("Error adding remote: %v", err) t.Errorf("Error adding remote: %v", err)
} }
@@ -35,5 +35,11 @@ func TestConnection(t *testing.T) {
t.Errorf("Expected data, got nil") t.Errorf("Expected data, got nil")
} }
time.Sleep(2 * time.Millisecond) time.Sleep(2 * time.Millisecond)
data, err = pool.Get(ToCartId("kalle"))
if err != nil {
t.Errorf("Error getting data: %v", err)
}
if data == nil {
t.Errorf("Expected data, got nil")
}
} }

View File

@@ -57,6 +57,7 @@ func (m *PersistentConnection) Connect() error {
if !m.Dead { if !m.Dead {
connection, err := net.Dial("tcp", m.address) connection, err := net.Dial("tcp", m.address)
if err != nil { if err != nil {
log.Printf("Error connecting to %s: %v\n", m.address, err)
m.Died <- true m.Died <- true
m.Dead = true m.Dead = true
return err return err
@@ -74,6 +75,7 @@ func (m *PersistentConnection) Close() {
func (m *PersistentConnection) HandleConnectionError(err error) error { func (m *PersistentConnection) HandleConnectionError(err error) error {
if err != nil { if err != nil {
log.Printf("Error from to %s: %v\n", m.address, err)
m.Conn.Close() m.Conn.Close()
m.Connect() m.Connect()
} }