(Feat): Initial Commit
Some checks failed
Build & Test / build-test (push) Has been cancelled
Build & Test / swagger-codegen-cli (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled

This commit is contained in:
2026-07-03 19:41:31 +05:30
commit 7e940c83a7
461 changed files with 45002 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
package util_test
import (
"context"
"testing"
"time"
"allaboutapps.dev/aw/go-starter/internal/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type contextKey string
func TestDetachContextWithCancel(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
var key contextKey = "test"
val := 42
ctx2 := context.WithValue(ctx, key, val)
detachedContext := util.DetachContext(ctx2)
cancel()
select {
case <-ctx.Done():
t.Log("Context cancelled")
default:
t.Error("Context is not canceled")
}
select {
case <-ctx2.Done():
t.Log("Context with value cancelled")
default:
t.Error("Context with value is not canceled")
}
select {
case <-detachedContext.Done():
t.Error("Detached context is cancelled")
default:
t.Log("Detached context is not cancelled")
}
res, ok := detachedContext.Value(key).(int)
require.True(t, ok)
assert.Equal(t, val, res)
}
func TestDetachContextWithDeadline(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), time.Second*1)
defer cancel()
var key contextKey = "test"
val := 42
ctx2 := context.WithValue(ctx, key, val)
detachedContext := util.DetachContext(ctx2)
time.Sleep(time.Second * 2)
select {
case <-ctx.Done():
t.Log("Context cancelled")
default:
t.Error("Context is not canceled")
}
select {
case <-ctx2.Done():
t.Log("Context with value cancelled")
default:
t.Error("Context with value is not canceled")
}
select {
case <-detachedContext.Done():
t.Error("Detached context is cancelled")
default:
t.Log("Detached context is not cancelled")
}
res, ok := detachedContext.Value(key).(int)
require.True(t, ok)
assert.Equal(t, val, res)
}