mirror of
https://github.com/tiennm99/goclaw.git
synced 2026-06-10 10:10:49 +00:00
6895e369f6
- Remove standalone mode code: file-based stores, standalone gateway, heartbeat service, SQLite memory, standalone docker-compose - Rename docker-compose.managed.yml → docker-compose.postgres.yml - Clean up ~130 Go comments referencing "managed mode" qualifier - Simplify docker-compose.yml env vars (providers/channels via web UI) - Update .env.example to essential vars only (token + encryption key) - Add setup wizard UI (provider → agent → channel bootstrap flow) - Add logs.tail WebSocket handler for live log streaming - Add cursor-pointer to interactive UI components - Clean up config page (remove standalone-only sections) - Update README and docs for managed-only architecture
93 lines
1.8 KiB
Go
93 lines
1.8 KiB
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestInMemoryCache_GetSet(t *testing.T) {
|
|
c := NewInMemoryCache[string]()
|
|
ctx := context.Background()
|
|
|
|
c.Set(ctx, "hello", "world", 0)
|
|
val, ok := c.Get(ctx, "hello")
|
|
if !ok {
|
|
t.Fatal("expected key to exist")
|
|
}
|
|
if val != "world" {
|
|
t.Fatalf("expected 'world', got %q", val)
|
|
}
|
|
}
|
|
|
|
func TestInMemoryCache_TTLExpiry(t *testing.T) {
|
|
c := NewInMemoryCache[int]()
|
|
ctx := context.Background()
|
|
|
|
c.Set(ctx, "key", 42, 20*time.Millisecond)
|
|
|
|
// Should be present immediately
|
|
val, ok := c.Get(ctx, "key")
|
|
if !ok || val != 42 {
|
|
t.Fatal("expected key to be present before TTL expiry")
|
|
}
|
|
|
|
time.Sleep(40 * time.Millisecond)
|
|
|
|
_, ok = c.Get(ctx, "key")
|
|
if ok {
|
|
t.Fatal("expected key to be expired after TTL")
|
|
}
|
|
}
|
|
|
|
func TestInMemoryCache_Delete(t *testing.T) {
|
|
c := NewInMemoryCache[string]()
|
|
ctx := context.Background()
|
|
|
|
c.Set(ctx, "k", "v", 0)
|
|
c.Delete(ctx, "k")
|
|
|
|
_, ok := c.Get(ctx, "k")
|
|
if ok {
|
|
t.Fatal("expected key to be deleted")
|
|
}
|
|
}
|
|
|
|
func TestInMemoryCache_DeleteByPrefix(t *testing.T) {
|
|
c := NewInMemoryCache[string]()
|
|
ctx := context.Background()
|
|
|
|
c.Set(ctx, "agent:1:files", "a", 0)
|
|
c.Set(ctx, "agent:1:meta", "b", 0)
|
|
c.Set(ctx, "agent:2:files", "c", 0)
|
|
|
|
c.DeleteByPrefix(ctx, "agent:1:")
|
|
|
|
if _, ok := c.Get(ctx, "agent:1:files"); ok {
|
|
t.Error("agent:1:files should be deleted")
|
|
}
|
|
if _, ok := c.Get(ctx, "agent:1:meta"); ok {
|
|
t.Error("agent:1:meta should be deleted")
|
|
}
|
|
if _, ok := c.Get(ctx, "agent:2:files"); !ok {
|
|
t.Error("agent:2:files should still exist")
|
|
}
|
|
}
|
|
|
|
func TestInMemoryCache_Clear(t *testing.T) {
|
|
c := NewInMemoryCache[int]()
|
|
ctx := context.Background()
|
|
|
|
c.Set(ctx, "a", 1, 0)
|
|
c.Set(ctx, "b", 2, 0)
|
|
c.Set(ctx, "c", 3, 0)
|
|
|
|
c.Clear(ctx)
|
|
|
|
for _, k := range []string{"a", "b", "c"} {
|
|
if _, ok := c.Get(ctx, k); ok {
|
|
t.Errorf("key %q should be cleared", k)
|
|
}
|
|
}
|
|
}
|