[🤖] Add Unit-Tests
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m34s

This commit is contained in:
2026-04-27 10:46:08 +02:00
parent dad0e3240d
commit 02d6894ec6
116 changed files with 18795 additions and 1 deletions
+213
View File
@@ -0,0 +1,213 @@
package syncext
import (
"context"
"sync"
"testing"
"time"
)
func TestAtomicGetSet(t *testing.T) {
a := NewAtomic(42)
if v := a.Get(); v != 42 {
t.Errorf("expected 42, got %d", v)
}
old := a.Set(100)
if old != 42 {
t.Errorf("expected old value 42, got %d", old)
}
if v := a.Get(); v != 100 {
t.Errorf("expected 100, got %d", v)
}
}
func TestAtomicGetSetString(t *testing.T) {
a := NewAtomic("hello")
if v := a.Get(); v != "hello" {
t.Errorf("expected 'hello', got %q", v)
}
old := a.Set("world")
if old != "hello" {
t.Errorf("expected old value 'hello', got %q", old)
}
if v := a.Get(); v != "world" {
t.Errorf("expected 'world', got %q", v)
}
}
func TestAtomicUpdate(t *testing.T) {
a := NewAtomic(10)
a.Update(func(old int) int {
return old * 2
})
if v := a.Get(); v != 20 {
t.Errorf("expected 20, got %d", v)
}
a.Update(func(old int) int {
return old + 5
})
if v := a.Get(); v != 25 {
t.Errorf("expected 25, got %d", v)
}
}
func TestAtomicCompareAndSwap(t *testing.T) {
a := NewAtomic(5)
if !a.CompareAndSwap(5, 10) {
t.Error("CAS should have succeeded")
}
if v := a.Get(); v != 10 {
t.Errorf("expected 10, got %d", v)
}
if a.CompareAndSwap(5, 20) {
t.Error("CAS should have failed")
}
if v := a.Get(); v != 10 {
t.Errorf("expected 10, got %d", v)
}
}
func TestAtomicWaitAlreadyMatching(t *testing.T) {
a := NewAtomic(7)
done := make(chan struct{})
go func() {
a.Wait(7)
close(done)
}()
select {
case <-done:
// ok
case <-time.After(500 * time.Millisecond):
t.Error("Wait should return immediately if value already matches")
}
}
func TestAtomicWaitWithTimeoutNoMatch(t *testing.T) {
a := NewAtomic(1)
err := a.WaitWithTimeout(50*time.Millisecond, 999)
if err == nil {
t.Error("expected timeout error")
}
}
func TestAtomicWaitWithTimeoutMatchAfterSet(t *testing.T) {
a := NewAtomic(1)
go func() {
time.Sleep(20 * time.Millisecond)
a.Set(99)
}()
err := a.WaitWithTimeout(500*time.Millisecond, 99)
if err != nil {
t.Errorf("expected nil, got %v", err)
}
}
func TestAtomicWaitWithContextCancel(t *testing.T) {
a := NewAtomic(1)
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(20 * time.Millisecond)
cancel()
}()
err := a.WaitWithContext(ctx, 999)
if err == nil {
t.Error("expected ctx error")
}
}
func TestAtomicWaitWithContextAlreadyCancelled(t *testing.T) {
a := NewAtomic(1)
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := a.WaitWithContext(ctx, 1)
if err == nil {
t.Error("expected ctx error")
}
}
func TestAtomicWaitForChange(t *testing.T) {
a := NewAtomic(1)
ch := a.WaitForChange()
go func() {
time.Sleep(20 * time.Millisecond)
a.Set(2)
}()
select {
case v := <-ch:
if v != 2 {
t.Errorf("expected 2, got %d", v)
}
case <-time.After(500 * time.Millisecond):
t.Error("WaitForChange did not deliver")
}
}
func TestAtomicConcurrentSet(t *testing.T) {
a := NewAtomic(0)
var wg sync.WaitGroup
for i := 0; i < 50; i++ {
wg.Add(1)
go func(v int) {
defer wg.Done()
a.Set(v)
}(i)
}
wg.Wait()
v := a.Get()
if v < 0 || v >= 50 {
t.Errorf("unexpected final value %d", v)
}
}
func TestAtomicConcurrentUpdate(t *testing.T) {
a := NewAtomic(0)
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
a.Update(func(old int) int { return old + 1 })
}()
}
wg.Wait()
if v := a.Get(); v != 100 {
t.Errorf("expected 100, got %d", v)
}
}
func TestAtomicWaitWithTimeoutZero(t *testing.T) {
a := NewAtomic(1)
err := a.WaitWithTimeout(0, 999)
if err == nil {
t.Error("expected error for zero timeout with non-matching value")
}
}
+124
View File
@@ -0,0 +1,124 @@
package syncext
import (
"context"
"sync"
"testing"
"time"
)
func TestAtomicBoolGetSet(t *testing.T) {
b := NewAtomicBool(false)
if b.Get() {
t.Error("expected false")
}
old := b.Set(true)
if old {
t.Error("expected old value false")
}
if !b.Get() {
t.Error("expected true")
}
old = b.Set(false)
if !old {
t.Error("expected old value true")
}
}
func TestAtomicBoolWaitAlreadyMatching(t *testing.T) {
b := NewAtomicBool(true)
done := make(chan struct{})
go func() {
b.Wait(true)
close(done)
}()
select {
case <-done:
// ok
case <-time.After(500 * time.Millisecond):
t.Error("Wait should return immediately if value already matches")
}
}
func TestAtomicBoolWaitWithTimeoutNoMatch(t *testing.T) {
b := NewAtomicBool(false)
err := b.WaitWithTimeout(50*time.Millisecond, true)
if err == nil {
t.Error("expected timeout error")
}
}
func TestAtomicBoolWaitWithTimeoutMatchAfterSet(t *testing.T) {
b := NewAtomicBool(false)
go func() {
time.Sleep(20 * time.Millisecond)
b.Set(true)
}()
err := b.WaitWithTimeout(500*time.Millisecond, true)
if err != nil {
t.Errorf("expected nil, got %v", err)
}
}
func TestAtomicBoolWaitWithContextCancel(t *testing.T) {
b := NewAtomicBool(false)
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(20 * time.Millisecond)
cancel()
}()
err := b.WaitWithContext(ctx, true)
if err == nil {
t.Error("expected ctx error")
}
}
func TestAtomicBoolWaitWithContextAlreadyCancelled(t *testing.T) {
b := NewAtomicBool(false)
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := b.WaitWithContext(ctx, false)
if err == nil {
t.Error("expected ctx error")
}
}
func TestAtomicBoolWaitWithContextMatching(t *testing.T) {
b := NewAtomicBool(true)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
err := b.WaitWithContext(ctx, true)
if err != nil {
t.Errorf("expected nil, got %v", err)
}
}
func TestAtomicBoolConcurrentSet(t *testing.T) {
b := NewAtomicBool(false)
var wg sync.WaitGroup
for i := 0; i < 50; i++ {
wg.Add(1)
go func(v bool) {
defer wg.Done()
b.Set(v)
}(i%2 == 0)
}
wg.Wait()
}
+155
View File
@@ -0,0 +1,155 @@
package syncext
import (
"context"
"testing"
"time"
)
func TestWriteChannelWithTimeoutSuccess(t *testing.T) {
c := make(chan int, 1)
ok := WriteChannelWithTimeout(c, 42, 100*time.Millisecond)
if !ok {
t.Error("expected write to succeed")
}
select {
case v := <-c:
if v != 42 {
t.Errorf("expected 42, got %d", v)
}
default:
t.Error("no value received")
}
}
func TestWriteChannelWithTimeoutFull(t *testing.T) {
c := make(chan int, 1)
c <- 1
ok := WriteChannelWithTimeout(c, 2, 50*time.Millisecond)
if ok {
t.Error("expected write to timeout")
}
}
func TestWriteChannelWithTimeoutUnbuffered(t *testing.T) {
c := make(chan int)
go func() {
time.Sleep(10 * time.Millisecond)
<-c
}()
ok := WriteChannelWithTimeout(c, 99, 200*time.Millisecond)
if !ok {
t.Error("expected write to succeed")
}
}
func TestWriteChannelWithTimeoutUnbufferedTimeout(t *testing.T) {
c := make(chan int)
ok := WriteChannelWithTimeout(c, 99, 50*time.Millisecond)
if ok {
t.Error("expected timeout")
}
}
func TestWriteChannelWithContextSuccess(t *testing.T) {
c := make(chan int, 1)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
err := WriteChannelWithContext(ctx, c, 7)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if v := <-c; v != 7 {
t.Errorf("expected 7, got %d", v)
}
}
func TestWriteChannelWithContextCancel(t *testing.T) {
c := make(chan int)
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(20 * time.Millisecond)
cancel()
}()
err := WriteChannelWithContext(ctx, c, 7)
if err == nil {
t.Error("expected ctx error")
}
}
func TestWriteChannelWithContextAlreadyCancelled(t *testing.T) {
c := make(chan int)
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := WriteChannelWithContext(ctx, c, 7)
if err == nil {
t.Error("expected ctx error")
}
}
func TestReadNonBlockingEmpty(t *testing.T) {
c := make(chan int, 1)
_, ok := ReadNonBlocking(c)
if ok {
t.Error("expected non-blocking read to return false on empty channel")
}
}
func TestReadNonBlockingHasValue(t *testing.T) {
c := make(chan int, 1)
c <- 55
v, ok := ReadNonBlocking(c)
if !ok {
t.Error("expected non-blocking read to return true")
}
if v != 55 {
t.Errorf("expected 55, got %d", v)
}
}
func TestWriteNonBlockingSuccess(t *testing.T) {
c := make(chan int, 1)
ok := WriteNonBlocking(c, 33)
if !ok {
t.Error("expected non-blocking write to succeed")
}
if v := <-c; v != 33 {
t.Errorf("expected 33, got %d", v)
}
}
func TestWriteNonBlockingFull(t *testing.T) {
c := make(chan int, 1)
c <- 1
ok := WriteNonBlocking(c, 2)
if ok {
t.Error("expected non-blocking write to fail when full")
}
}
func TestWriteNonBlockingUnbufferedNoReceiver(t *testing.T) {
c := make(chan int)
ok := WriteNonBlocking(c, 1)
if ok {
t.Error("expected non-blocking write to fail without receiver")
}
}