mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-09-21 01:34:16 +00:00
87 lines
2.0 KiB
Go
87 lines
2.0 KiB
Go
package jetstream
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func TestEndpointRotator_Current(t *testing.T) {
|
|
r := NewEndpointRotator([]string{"a", "b", "c"})
|
|
if got := r.Current(); got != "a" {
|
|
t.Errorf("Current() = %q, want %q", got, "a")
|
|
}
|
|
// Calling Current again should not advance
|
|
if got := r.Current(); got != "a" {
|
|
t.Errorf("Current() second call = %q, want %q", got, "a")
|
|
}
|
|
}
|
|
|
|
func TestEndpointRotator_Next(t *testing.T) {
|
|
r := NewEndpointRotator([]string{"a", "b", "c"})
|
|
if got := r.Next(); got != "b" {
|
|
t.Errorf("Next() = %q, want %q", got, "b")
|
|
}
|
|
if got := r.Next(); got != "c" {
|
|
t.Errorf("Next() = %q, want %q", got, "c")
|
|
}
|
|
}
|
|
|
|
func TestEndpointRotator_WrapAround(t *testing.T) {
|
|
r := NewEndpointRotator([]string{"a", "b"})
|
|
r.Next() // -> b
|
|
got := r.Next()
|
|
if got != "a" {
|
|
t.Errorf("Next() after wrap = %q, want %q", got, "a")
|
|
}
|
|
}
|
|
|
|
func TestEndpointRotator_Reset(t *testing.T) {
|
|
r := NewEndpointRotator([]string{"a", "b", "c"})
|
|
r.Next() // -> b
|
|
r.Next() // -> c
|
|
r.Reset()
|
|
if got := r.Current(); got != "a" {
|
|
t.Errorf("Current() after Reset() = %q, want %q", got, "a")
|
|
}
|
|
}
|
|
|
|
func TestEndpointRotator_Len(t *testing.T) {
|
|
r := NewEndpointRotator([]string{"a", "b", "c"})
|
|
if got := r.Len(); got != 3 {
|
|
t.Errorf("Len() = %d, want 3", got)
|
|
}
|
|
}
|
|
|
|
func TestEndpointRotator_SingleEndpoint(t *testing.T) {
|
|
r := NewEndpointRotator([]string{"only"})
|
|
if got := r.Current(); got != "only" {
|
|
t.Errorf("Current() = %q, want %q", got, "only")
|
|
}
|
|
// Next on single endpoint wraps back to itself
|
|
if got := r.Next(); got != "only" {
|
|
t.Errorf("Next() = %q, want %q", got, "only")
|
|
}
|
|
}
|
|
|
|
func TestEndpointRotator_PanicsOnEmpty(t *testing.T) {
|
|
defer func() {
|
|
if r := recover(); r == nil {
|
|
t.Error("NewEndpointRotator(nil) did not panic")
|
|
}
|
|
}()
|
|
NewEndpointRotator(nil)
|
|
}
|
|
|
|
func TestEndpointRotator_Concurrent(t *testing.T) {
|
|
r := NewEndpointRotator([]string{"a", "b", "c", "d"})
|
|
var wg sync.WaitGroup
|
|
for range 100 {
|
|
wg.Go(func() {
|
|
_ = r.Current()
|
|
_ = r.Next()
|
|
})
|
|
}
|
|
wg.Wait()
|
|
// No race detector failures = success
|
|
}
|