change around pings, fix main.js so the actual real fucking number shows instead of it freaking out.
This commit is contained in:
@@ -76,6 +76,78 @@ func TestHandlePing(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlePingPreservesZeroTimestamp(t *testing.T) {
|
||||
writer := &testWriter{}
|
||||
env := &rt.Env{
|
||||
Conn: writer,
|
||||
Cfg: config.Config{},
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
envelope := map[string]interface{}{
|
||||
"ts": int64(0),
|
||||
}
|
||||
|
||||
if err := HandlePing(ctx, env, envelope); err != nil {
|
||||
t.Fatalf("HandlePing failed: %v", err)
|
||||
}
|
||||
|
||||
deadline := time.After(2 * time.Second)
|
||||
for len(writer.msgs) < 1 {
|
||||
select {
|
||||
case <-deadline:
|
||||
t.Fatal("Timed out waiting for pong message")
|
||||
default:
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
var pong wire.Pong
|
||||
if err := msgpack.Unmarshal(writer.msgs[0], &pong); err != nil {
|
||||
t.Fatalf("Failed to unmarshal pong: %v", err)
|
||||
}
|
||||
if pong.TS != 0 {
|
||||
t.Fatalf("expected timestamp 0, got %d", pong.TS)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlePingFallsBackForInvalidTimestamp(t *testing.T) {
|
||||
writer := &testWriter{}
|
||||
env := &rt.Env{
|
||||
Conn: writer,
|
||||
Cfg: config.Config{},
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
before := time.Now().UnixMilli()
|
||||
envelope := map[string]interface{}{
|
||||
"ts": "not-a-timestamp",
|
||||
}
|
||||
|
||||
if err := HandlePing(ctx, env, envelope); err != nil {
|
||||
t.Fatalf("HandlePing failed: %v", err)
|
||||
}
|
||||
|
||||
deadline := time.After(2 * time.Second)
|
||||
for len(writer.msgs) < 1 {
|
||||
select {
|
||||
case <-deadline:
|
||||
t.Fatal("Timed out waiting for pong message")
|
||||
default:
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
var pong wire.Pong
|
||||
if err := msgpack.Unmarshal(writer.msgs[0], &pong); err != nil {
|
||||
t.Fatalf("Failed to unmarshal pong: %v", err)
|
||||
}
|
||||
after := time.Now().UnixMilli()
|
||||
if pong.TS < before || pong.TS > after {
|
||||
t.Fatalf("expected fallback timestamp between %d and %d, got %d", before, after, pong.TS)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleCommand_Ping(t *testing.T) {
|
||||
writer := &testWriter{}
|
||||
env := &rt.Env{
|
||||
|
||||
@@ -3,36 +3,44 @@ package handlers
|
||||
import "encoding/json"
|
||||
|
||||
func extractTimestamp(v interface{}) int64 {
|
||||
ts, ok := extractTimestampIfPresent(v)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
return ts
|
||||
}
|
||||
|
||||
func extractTimestampIfPresent(v interface{}) (int64, bool) {
|
||||
switch t := v.(type) {
|
||||
case uint64:
|
||||
if t <= uint64(^uint64(0)>>1) {
|
||||
return int64(t)
|
||||
return int64(t), true
|
||||
}
|
||||
return 0
|
||||
return 0, false
|
||||
case uint32:
|
||||
return int64(t)
|
||||
return int64(t), true
|
||||
case uint16:
|
||||
return int64(t)
|
||||
return int64(t), true
|
||||
case uint8:
|
||||
return int64(t)
|
||||
return int64(t), true
|
||||
case int64:
|
||||
return t
|
||||
return t, true
|
||||
case int32:
|
||||
return int64(t)
|
||||
return int64(t), true
|
||||
case int16:
|
||||
return int64(t)
|
||||
return int64(t), true
|
||||
case int8:
|
||||
return int64(t)
|
||||
return int64(t), true
|
||||
case int:
|
||||
return int64(t)
|
||||
return int64(t), true
|
||||
case float64:
|
||||
return int64(t)
|
||||
return int64(t), true
|
||||
case float32:
|
||||
return int64(t)
|
||||
return int64(t), true
|
||||
case json.Number:
|
||||
if n, err := t.Int64(); err == nil {
|
||||
return n
|
||||
return n, true
|
||||
}
|
||||
}
|
||||
return 0
|
||||
return 0, false
|
||||
}
|
||||
|
||||
@@ -129,6 +129,22 @@ func TestExtractTimestamp_Zero(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractTimestampIfPresent_ZeroIsValid(t *testing.T) {
|
||||
got, ok := extractTimestampIfPresent(int64(0))
|
||||
if !ok {
|
||||
t.Fatal("expected zero timestamp to be valid")
|
||||
}
|
||||
if got != 0 {
|
||||
t.Fatalf("expected 0, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractTimestampIfPresent_InvalidIsNotValid(t *testing.T) {
|
||||
if got, ok := extractTimestampIfPresent("hello"); ok || got != 0 {
|
||||
t.Fatalf("expected invalid timestamp to return ok=false and 0, got ok=%v value=%d", ok, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractTimestamp_Negative(t *testing.T) {
|
||||
var ts int64 = -100
|
||||
if got := extractTimestamp(ts); got != ts {
|
||||
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
|
||||
func HandlePing(ctx context.Context, env *runtime.Env, envelope map[string]interface{}) error {
|
||||
|
||||
ts := extractTimestamp(envelope["ts"])
|
||||
if ts == 0 {
|
||||
ts, ok := extractTimestampIfPresent(envelope["ts"])
|
||||
if !ok {
|
||||
ts = time.Now().UnixMilli()
|
||||
}
|
||||
env.SetLastPong(time.Now().UnixMilli())
|
||||
|
||||
Reference in New Issue
Block a user