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/d0"
  6. )
  7. type tester struct {
  8. t *testing.T
  9. lCtx *LocalCtx
  10. owner *d0.LOwner
  11. }
  12. func TestLocalCtx(t *testing.T) {
  13. sf := &tester{
  14. t: t,
  15. owner: d0.LetOwner(1, "test_local_ctx"),
  16. }
  17. sf.new()
  18. sf.set()
  19. sf.get()
  20. sf.del()
  21. sf.done()
  22. }
  23. // Ожидает отмены контекста.
  24. func (sf *tester) done() {
  25. sf.t.Log("done")
  26. _ = sf.lCtx.Size()
  27. go sf.lCtx.Cancel()
  28. sf.lCtx.Wait()
  29. }
  30. // Удаляет несуществующий ключ из локального контекста.
  31. func (sf *tester) del() {
  32. sf.t.Log("del")
  33. sf.lCtx.Del(d0.LetTxt("123"))
  34. sf.lCtx.Del(d0.LetTxt("count"))
  35. }
  36. // Возвращает хранимое значение.
  37. func (sf *tester) get() {
  38. sf.t.Log("get")
  39. optRes := sf.lCtx.Get(d0.LetTxt("count"))
  40. ctxVal := optRes.Hassert("get()")
  41. count := ctxVal.Val().(d0.Num)
  42. if count == 15 {
  43. return
  44. }
  45. }
  46. // Устанавливает значение.
  47. func (sf *tester) set() {
  48. sf.t.Log("set")
  49. sf.lCtx.Set(d0.LetTxt("count"), 5, "test_val")
  50. sf.lCtx.Set(d0.LetTxt("count"), 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(sf.owner, ctx)
  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.SelfCtx(); 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(sf.owner, ctx)
  80. }