kctx_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package kctx
  2. import (
  3. "testing"
  4. )
  5. type tester struct {
  6. t *testing.T
  7. }
  8. func TestKernelCtx(t *testing.T) {
  9. sf := &tester{
  10. t: t,
  11. }
  12. sf.new()
  13. }
  14. // Создание контекста ядра
  15. func (sf *tester) new() {
  16. sf.t.Log("new")
  17. ctx := GetKernelCtx()
  18. if ctx == nil {
  19. sf.t.Fatalf("new(): KernelCtx==nil")
  20. }
  21. if ctx := ctx.CtxBg(); ctx != kernCtx.ctxBg {
  22. sf.t.Fatalf("new(): ctx!=ctxBg")
  23. }
  24. if ctx := ctx.Ctx(); ctx == nil {
  25. sf.t.Fatalf("new(): ctx==nil")
  26. }
  27. ctx.Set("counter", 5, "test_counter")
  28. if ctx.Get("counter") == nil {
  29. sf.t.Fatalf("new(): counter==nil")
  30. }
  31. res := ctx.Get("counter")
  32. ctxVal := res.Hassert("new()")
  33. val := ctxVal.Val().(int)
  34. if val != 5 {
  35. sf.t.Fatalf("new(): counter(%v)!=5", val)
  36. }
  37. ctx.Del("counter")
  38. ctx.Cancel()
  39. ctx.Done()
  40. ctx = GetKernelCtx()
  41. if ctx == nil {
  42. sf.t.Fatalf("new(): KernelCtx==nil")
  43. }
  44. if wg := ctx.Wg(); wg == nil {
  45. sf.t.Fatalf("new(): IKernelWg==nil")
  46. }
  47. if keep := ctx.Keeper(); keep == nil {
  48. sf.t.Fatalf("new(): IKernelKeeper==nil")
  49. }
  50. }