68 lines
2.1 KiB
Go
68 lines
2.1 KiB
Go
|
|
package notify
|
||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestPendingWaitPoolDeliverWithScope(t *testing.T) {
|
||
|
|
pool := newPendingWaitPool()
|
||
|
|
wait := pool.createAndStoreWithScope(TransferMsg{ID: 11, Key: "k", Type: MSG_SYNC_ASK}, "scope-a")
|
||
|
|
if delivered := pool.deliverWithScopes(11, []string{"scope-b"}, Message{}); delivered {
|
||
|
|
t.Fatal("deliverWithScopes should reject mismatched scope")
|
||
|
|
}
|
||
|
|
msg := Message{TransferMsg: TransferMsg{ID: 11, Key: "k", Type: MSG_SYNC_REPLY}}
|
||
|
|
if delivered := pool.deliverWithScopes(11, []string{"scope-a"}, msg); !delivered {
|
||
|
|
t.Fatal("deliverWithScopes should accept matching scope")
|
||
|
|
}
|
||
|
|
select {
|
||
|
|
case got := <-wait.Reply:
|
||
|
|
if got.ID != msg.ID || got.Type != msg.Type {
|
||
|
|
t.Fatalf("delivered message mismatch: got=%+v want=%+v", got.TransferMsg, msg.TransferMsg)
|
||
|
|
}
|
||
|
|
default:
|
||
|
|
t.Fatal("wait reply should receive delivered message")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPendingWaitPoolCloseScopeClosesReplies(t *testing.T) {
|
||
|
|
pool := newPendingWaitPool()
|
||
|
|
wait := pool.createAndStoreWithScope(TransferMsg{ID: 12, Key: "k", Type: MSG_SYNC_ASK}, "scope-close")
|
||
|
|
pool.closeScope("scope-close")
|
||
|
|
select {
|
||
|
|
case _, ok := <-wait.Reply:
|
||
|
|
if ok {
|
||
|
|
t.Fatal("wait reply channel should be closed")
|
||
|
|
}
|
||
|
|
case <-time.After(time.Second):
|
||
|
|
t.Fatal("timed out waiting for closed wait reply")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestSignalAckPoolDeliverAny(t *testing.T) {
|
||
|
|
pool := newSignalAckPool()
|
||
|
|
wait := pool.prepare("scope-a", 21)
|
||
|
|
if delivered := pool.deliverAny([]string{"scope-b"}, 21); delivered {
|
||
|
|
t.Fatal("deliverAny should reject mismatched scope")
|
||
|
|
}
|
||
|
|
go func() {
|
||
|
|
time.Sleep(10 * time.Millisecond)
|
||
|
|
_ = pool.deliverAny([]string{"scope-a"}, 21)
|
||
|
|
}()
|
||
|
|
if err := pool.waitPrepared(wait, time.Second); err != nil {
|
||
|
|
t.Fatalf("waitPrepared failed: %v", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestSignalAckPoolCloseScopeCancelsWait(t *testing.T) {
|
||
|
|
pool := newSignalAckPool()
|
||
|
|
wait := pool.prepare("scope-close", 22)
|
||
|
|
go func() {
|
||
|
|
time.Sleep(10 * time.Millisecond)
|
||
|
|
pool.closeScope("scope-close")
|
||
|
|
}()
|
||
|
|
if err := pool.waitPrepared(wait, time.Second); err != errSignalAckCanceled {
|
||
|
|
t.Fatalf("waitPrepared error = %v, want %v", err, errSignalAckCanceled)
|
||
|
|
}
|
||
|
|
}
|