feat: 新增 XYZ 图层使用人数统计服务
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRecorderDedup(t *testing.T) {
|
||||
r := NewRecorder()
|
||||
m0 := time.Unix(100*60, 0)
|
||||
m1 := time.Unix(101*60, 0)
|
||||
|
||||
r.Record(m0, "grid", "sid", "fp")
|
||||
r.Record(m0, "grid", "sid", "fp")
|
||||
hits, sessions := r.Take()
|
||||
if len(hits) != 1 {
|
||||
t.Fatalf("same minute rows %d", len(hits))
|
||||
}
|
||||
if sessions["sid"] == nil || sessions["sid"].first != 100 || sessions["sid"].last != 100 {
|
||||
t.Fatalf("session %+v", sessions["sid"])
|
||||
}
|
||||
|
||||
r.Record(m0, "grid", "sid", "fp")
|
||||
hits, _ = r.Take()
|
||||
if len(hits) != 0 {
|
||||
t.Fatalf("duplicate between takes wrote %d", len(hits))
|
||||
}
|
||||
|
||||
r.Record(m0, "grid", "other", "fp")
|
||||
r.Record(m1, "grid", "other", "fp")
|
||||
hits, sessions = r.Take()
|
||||
if len(hits) != 2 {
|
||||
t.Fatalf("cross minute rows %d", len(hits))
|
||||
}
|
||||
if sessions["other"] == nil || sessions["other"].first != 100 || sessions["other"].last != 101 {
|
||||
t.Fatalf("cross minute session %+v", sessions["other"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecorderAnonymousSID(t *testing.T) {
|
||||
r := NewRecorder()
|
||||
now := time.Unix(100*60, 0)
|
||||
r.Record(now, "grid", "", "abcd")
|
||||
hits, sessions := r.Take()
|
||||
if len(hits) != 1 || hits[0].sid != "~abcd:100" {
|
||||
t.Fatalf("hits %+v", hits)
|
||||
}
|
||||
if sessions["~abcd:100"] == nil || sessions["~abcd:100"].fp != "abcd" {
|
||||
t.Fatalf("session %+v", sessions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecorderLimit(t *testing.T) {
|
||||
r := NewRecorder()
|
||||
now := time.Unix(1000*60, 0)
|
||||
for i := 0; i < maxRowsPerMinute; i++ {
|
||||
r.Record(now, "t", strconv.Itoa(i), "fp")
|
||||
}
|
||||
if r.Dropped() != 0 {
|
||||
t.Fatalf("dropped %d before overflow", r.Dropped())
|
||||
}
|
||||
r.Record(now, "t", "0", "fp")
|
||||
if r.Dropped() != 0 {
|
||||
t.Fatal("duplicate should not count as dropped")
|
||||
}
|
||||
r.Record(now, "t", "overflow", "fp")
|
||||
if r.Dropped() != 1 {
|
||||
t.Fatalf("dropped %d", r.Dropped())
|
||||
}
|
||||
hits, _ := r.Take()
|
||||
if len(hits) != maxRowsPerMinute {
|
||||
t.Fatalf("kept %d", len(hits))
|
||||
}
|
||||
|
||||
r.Record(now.Add(time.Minute), "t", "overflow", "fp")
|
||||
hits, _ = r.Take()
|
||||
if len(hits) != 1 {
|
||||
t.Fatalf("next minute %d", len(hits))
|
||||
}
|
||||
if r.Dropped() != 1 {
|
||||
t.Fatalf("dropped changed to %d", r.Dropped())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user