57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package memory
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/longpeng/configcenter/internal/domain"
|
|
runtimepkg "github.com/longpeng/configcenter/internal/runtime"
|
|
)
|
|
|
|
func TestWatchReportsCompactedRevision(t *testing.T) {
|
|
store := New()
|
|
ctx := context.Background()
|
|
payload, err := json.Marshal(map[string]string{"feature": "value"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for version := 1; version <= 257; version++ {
|
|
if _, err := store.Put(ctx, "/config/PROD/orders/application", payload, domain.Release{Version: version}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
result := <-store.Watch(ctx, "/config/PROD/orders/application", 1)
|
|
if !errors.Is(result.Err, runtimepkg.ErrRevisionCompacted) {
|
|
t.Fatalf("expected compacted error, got %#v", result)
|
|
}
|
|
if result.CompactRevision != 1 {
|
|
t.Fatalf("unexpected compact revision: %d", result.CompactRevision)
|
|
}
|
|
}
|
|
|
|
func TestWatchReplaysEveryAvailableRevision(t *testing.T) {
|
|
store := New()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
for version := 1; version <= 20; version++ {
|
|
payload, err := json.Marshal(map[string]string{"version": string(rune('A' + version - 1))})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := store.Put(ctx, "/config/PROD/orders/application", payload, domain.Release{Version: version}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
stream := store.Watch(ctx, "/config/PROD/orders/application", 5)
|
|
for revision := int64(5); revision <= 20; revision++ {
|
|
result := <-stream
|
|
if result.Err != nil || result.Event.Revision != revision {
|
|
t.Fatalf("unexpected replay at revision %d: %#v", revision, result)
|
|
}
|
|
}
|
|
}
|