local_ctx_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package local_ctx
  2. import (
  3. "context"
  4. "testing"
  5. "gitp78su.ipnodns.ru/svi/kern/v4/lev0"
  6. "gitp78su.ipnodns.ru/svi/kern/v4/lev0/defs"
  7. )
  8. type tester struct {
  9. t *testing.T
  10. lCtx *LocalCtx
  11. }
  12. func TestLocalCtx(t *testing.T) {
  13. sf := &tester{
  14. t: t,
  15. }
  16. sf.new()
  17. sf.set()
  18. sf.get()
  19. sf.del()
  20. sf.done()
  21. }
  22. // Ожидает отмены контекста.
  23. func (sf *tester) done() {
  24. sf.t.Log("done")
  25. _ = sf.lCtx.Size()
  26. go sf.lCtx.Cancel()
  27. sf.lCtx.Wait()
  28. }
  29. // Удаляет несуществующий ключ из локального контекста.
  30. func (sf *tester) del() {
  31. sf.t.Log("del")
  32. sf.lCtx.Del(lev0.LetTxt("123"))
  33. sf.lCtx.Del(lev0.LetTxt("count"))
  34. }
  35. // Возвращает хранимое значение.
  36. func (sf *tester) get() {
  37. sf.t.Log("get")
  38. optRes := sf.lCtx.Get(lev0.LetTxt("count"))
  39. ctxVal := optRes.Hassert("get()")
  40. count := ctxVal.Val().(defs.Int)
  41. if count == 15 {
  42. return
  43. }
  44. }
  45. // Устанавливает значение.
  46. func (sf *tester) set() {
  47. sf.t.Log("set")
  48. msg := lev0.LetTxt("count")
  49. sf.lCtx.Set(msg, 5, "test_val")
  50. sf.lCtx.Set(msg, 15, "test_val1")
  51. }
  52. // Создание нового локального контекста.
  53. func (sf *tester) new() {
  54. sf.t.Log("new")
  55. sf.newBad1()
  56. sf.newGood1()
  57. }
  58. func (sf *tester) newGood1() {
  59. sf.t.Log("newGood1")
  60. ctx := context.Background()
  61. sf.lCtx = NewLocalCtx(ctx).(*LocalCtx)
  62. _ = sf.lCtx.Log()
  63. if lst := sf.lCtx.SortedList(); lst == nil {
  64. sf.t.Fatalf("newGood1(): lst==nil")
  65. }
  66. if ctx := sf.lCtx.Ctx(); ctx == nil {
  67. sf.t.Fatalf("newGood1(): ctx==nil")
  68. }
  69. }
  70. // Нет контекста ядра.
  71. func (sf *tester) newBad1() {
  72. sf.t.Log("newBad1")
  73. defer func() {
  74. if _panic := recover(); _panic == nil {
  75. sf.t.Fatalf("newBad1(): panic==nil")
  76. }
  77. }()
  78. var ctx context.Context
  79. sf.lCtx = NewLocalCtx(ctx).(*LocalCtx)
  80. }