local_ctx_test.go 1.7 KB

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