156 lines
2.9 KiB
Go
156 lines
2.9 KiB
Go
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")
|
|
}
|
|
}
|