57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package federation
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestCursorIsMonotonicAndAuthenticationIsRequired(t *testing.T) {
|
|
r := &Registry{}
|
|
if err := r.Register(Worker{ID: "workpc", Token: "secret"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := r.Authenticate("workpc", "wrong"); err != ErrUnauthorized {
|
|
t.Fatalf("got %v", err)
|
|
}
|
|
if err := r.Authenticate("workpc", "secret"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := r.Ack("workpc", 7); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := r.Ack("workpc", 6); err == nil {
|
|
t.Fatal("backwards cursor accepted")
|
|
}
|
|
if got, _ := r.Cursor("workpc"); got != 7 {
|
|
t.Fatalf("cursor = %d", got)
|
|
}
|
|
}
|
|
|
|
func TestOfflineHookRunsOnceOnTransition(t *testing.T) {
|
|
called := make(chan Worker, 1)
|
|
r := &Registry{TTL: time.Millisecond, OnOffline: func(w Worker) { called <- w }}
|
|
if err := r.Register(Worker{ID: "workpc"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.mu.Lock()
|
|
w := r.workers["workpc"]
|
|
w.LastSeen = time.Now().Add(-time.Second)
|
|
r.workers["workpc"] = w
|
|
r.mu.Unlock()
|
|
r.Snapshot()
|
|
select {
|
|
case got := <-called:
|
|
if got.ID != "workpc" {
|
|
t.Fatal(got.ID)
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Fatal("offline hook not called")
|
|
}
|
|
r.Snapshot()
|
|
select {
|
|
case <-called:
|
|
t.Fatal("offline hook called twice")
|
|
case <-time.After(10 * time.Millisecond):
|
|
}
|
|
}
|