package testing
import (
"context"
"os"
stdtest "testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
amhelp "github.com/pancsta/asyncmachine-go/pkg/helpers"
am "github.com/pancsta/asyncmachine-go/pkg/machine"
"github.com/pancsta/asyncmachine-go/pkg/telemetry"
)
func MachDebug (t *stdtest .T , mach am .Api , amDbgAddr string ,
logLvl am .LogLevel , stdout bool ,
) {
if stdout {
mach .SemLogger ().SetSimple (t .Logf , logLvl )
} else if amDbgAddr == "" {
mach .SemLogger ().SetSimple (t .Logf , logLvl )
return
}
if amDbgAddr == "1" {
amDbgAddr = telemetry .DbgAddr
}
err := amhelp .MachDebug (mach , amDbgAddr , logLvl , stdout ,
amhelp .SemConfigEnv (true ))
require .NoError (t , err )
}
func MachDebugEnv (t *stdtest .T , mach am .Api ) {
amDbgAddr := os .Getenv (telemetry .EnvAmDbgAddr )
logLvl := am .EnvLogLevel ("" )
stdout := os .Getenv (amhelp .EnvAmLogPrint ) != ""
MachDebug (t , mach , amDbgAddr , logLvl , stdout )
}
func Wait (
t *stdtest .T , errMsg string , ctx context .Context , length time .Duration ,
) {
if !amhelp .Wait (ctx , length ) {
if t .Context ().Err () == nil {
t .Fatal ("ctx expired" )
}
}
}
func WaitForAll (
t *stdtest .T , source string , ctx context .Context , timeout time .Duration ,
chans ...<-chan struct {},
) {
if err := amhelp .WaitForAll (ctx , timeout , chans ...); err != nil {
if t .Context ().Err () == nil {
t .Fatal ("error for " + source + ": " + err .Error())
}
}
}
func WaitForErrAll (
t *stdtest .T , source string , ctx context .Context , mach am .Api ,
timeout time .Duration , chans ...<-chan struct {},
) {
if err := amhelp .WaitForErrAll (ctx , timeout , mach , chans ...); err != nil {
if t .Context ().Err () == nil {
t .Fatal ("error for " + source + ": " + err .Error())
}
}
}
func WaitForAny (
t *stdtest .T , source string , ctx context .Context , timeout time .Duration ,
chans ...<-chan struct {},
) {
if err := amhelp .WaitForAny (ctx , timeout , chans ...); err != nil {
if t .Context ().Err () == nil {
t .Fatal ("error for " + source + ": " + err .Error())
}
}
}
func GroupWhen1 (
t *stdtest .T , mach []am .Api , state string , ctx context .Context ,
) []<-chan struct {} {
chs , err := amhelp .GroupWhen1 (mach , state , ctx )
if err != nil {
if t .Context ().Err () == nil {
t .Fatal (err )
}
}
return chs
}
func AssertIs (t *stdtest .T , mach am .Api , states am .S ) {
assert .Subset (t , mach .ActiveStates (nil ), states , "%s expected" , states )
}
func AssertIs1 (t *stdtest .T , mach am .Api , state string ) {
assert .Subset (t , mach .ActiveStates (nil ), am .S {state }, "%s expected" , state )
}
func AssertNot (t *stdtest .T , mach am .Api , states am .S ) {
assert .NotSubset (t , mach .ActiveStates (nil ), states , "%s not expected" , states )
}
func AssertNot1 (t *stdtest .T , mach am .Api , state string ) {
assert .NotSubset (t , mach .ActiveStates (nil ), am .S {state }, "%s not expected" ,
state )
}
func AssertNoErrNow (t *stdtest .T , mach am .Api ) {
if mach .IsErr () && t .Context ().Err () == nil {
err := mach .Err ()
if err != nil {
t .Fatalf ("Unexpected error in %s: %s" , mach .Id (), err .Error())
} else {
t .Fatalf ("Unexpected error in %s" , mach .Id ())
}
}
}
func AssertNoErrEver (t *stdtest .T , mach am .Api ) {
if mach .Tick (am .StateException ) > 0 && t .Context ().Err () == nil {
err := mach .Err ()
if err != nil {
t .Fatalf ("Unexpected error in %s" , mach .Id ())
} else {
t .Fatalf ("Unexpected PAST error in %s" , mach .Id ())
}
}
}
func AssertErr (t *stdtest .T , mach am .Api ) {
if !mach .IsErr () && t .Context ().Err () == nil {
t .Fatal ("expected " + am .StateException )
}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .