notify/client_session_epoch_test.go

82 lines
2.8 KiB
Go
Raw Normal View History

package notify
import (
"context"
"errors"
"testing"
)
func TestClientStopSessionIfCurrentEpoch(t *testing.T) {
client := NewClient().(*ClientCommon)
client.markSessionStarted()
staleEpoch := client.beginClientSessionEpoch()
currentEpoch := client.beginClientSessionEpoch()
if client.stopClientSessionIfCurrent(staleEpoch, "stale", nil) {
t.Fatal("stale epoch should not stop current session")
}
status := client.Status()
if !status.Alive || status.Reason != "" || status.Err != nil {
t.Fatalf("unexpected status after stale stop: %+v", status)
}
if !client.stopClientSessionIfCurrent(currentEpoch, "current", nil) {
t.Fatal("current epoch should stop session")
}
status = client.Status()
if status.Alive || status.Reason != "current" || status.Err != nil {
t.Fatalf("unexpected status after current stop: %+v", status)
}
}
func TestClientReadErrorWithStaleEpochDoesNotStopCurrentSession(t *testing.T) {
client := NewClient().(*ClientCommon)
client.markSessionStarted()
staleEpoch := client.beginClientSessionEpoch()
currentEpoch := client.beginClientSessionEpoch()
readErr := errors.New("read failed")
client.handleTransportReadResultWithSession(context.Background(), nil, nil, 0, nil, readErr, staleEpoch)
status := client.Status()
if !status.Alive || status.Reason != "" || status.Err != nil {
t.Fatalf("unexpected status after stale read error: %+v", status)
}
client.handleTransportReadResultWithSession(context.Background(), nil, nil, 0, nil, readErr, currentEpoch)
status = client.Status()
if status.Alive || status.Reason != "client read error" || !errors.Is(status.Err, readErr) {
t.Fatalf("unexpected status after current read error: %+v", status)
}
}
func TestHeartbeatFailureWithStaleEpochDoesNotStopCurrentSession(t *testing.T) {
client := NewClient().(*ClientCommon)
client.markSessionStarted()
staleEpoch := client.beginClientSessionEpoch()
currentEpoch := client.beginClientSessionEpoch()
heartbeatErr := errors.New("heartbeat failed")
failedCount, stop := client.handleHeartbeatResultWithSession(staleEpoch, heartbeatErr, 2)
if failedCount != 3 || !stop {
t.Fatalf("unexpected stale heartbeat result: failedCount=%d stop=%v", failedCount, stop)
}
status := client.Status()
if !status.Alive || status.Reason != "" || status.Err != nil {
t.Fatalf("unexpected status after stale heartbeat error: %+v", status)
}
failedCount, stop = client.handleHeartbeatResultWithSession(currentEpoch, heartbeatErr, 2)
if failedCount != 3 || !stop {
t.Fatalf("unexpected current heartbeat result: failedCount=%d stop=%v", failedCount, stop)
}
status = client.Status()
if status.Alive || status.Reason != "heartbeat failed more than 3 times" || status.Err == nil || status.Err.Error() != "heartbeat failed more than 3 times" {
t.Fatalf("unexpected status after current heartbeat error: %+v", status)
}
}