[🤖] 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
+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")
}
}