| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package local_ctx
- import (
- "context"
- "testing"
- "gitp78su.ipnodns.ru/svi/kern/v4/d0"
- )
- type tester struct {
- t *testing.T
- lCtx *LocalCtx
- owner *d0.LOwner
- }
- func TestLocalCtx(t *testing.T) {
- sf := &tester{
- t: t,
- owner: d0.LetOwner(1, "test_local_ctx"),
- }
- sf.new()
- sf.set()
- sf.get()
- sf.del()
- sf.done()
- }
- // Ожидает отмены контекста.
- func (sf *tester) done() {
- sf.t.Log("done")
- _ = sf.lCtx.Size()
- go sf.lCtx.Cancel()
- sf.lCtx.Wait()
- }
- // Удаляет несуществующий ключ из локального контекста.
- func (sf *tester) del() {
- sf.t.Log("del")
- sf.lCtx.Del(d0.LetTxt("123"))
- sf.lCtx.Del(d0.LetTxt("count"))
- }
- // Возвращает хранимое значение.
- func (sf *tester) get() {
- sf.t.Log("get")
- optRes := sf.lCtx.Get(d0.LetTxt("count"))
- ctxVal := optRes.Hassert("get()")
- count := ctxVal.Val().(d0.Num)
- if count == 15 {
- return
- }
- }
- // Устанавливает значение.
- func (sf *tester) set() {
- sf.t.Log("set")
- sf.lCtx.Set(d0.LetTxt("count"), 5, "test_val")
- sf.lCtx.Set(d0.LetTxt("count"), 15, "test_val1")
- }
- // Создание нового локального контекста.
- func (sf *tester) new() {
- sf.t.Log("new")
- sf.newBad1()
- sf.newGood1()
- }
- func (sf *tester) newGood1() {
- sf.t.Log("newGood1")
- ctx := context.Background()
- sf.lCtx = NewLocalCtx(sf.owner, ctx)
- _ = sf.lCtx.Log()
- if lst := sf.lCtx.SortedList(); lst == nil {
- sf.t.Fatalf("newGood1(): lst==nil")
- }
- if ctx := sf.lCtx.SelfCtx(); ctx == nil {
- sf.t.Fatalf("newGood1(): ctx==nil")
- }
- }
- // Нет контекста ядра.
- func (sf *tester) newBad1() {
- sf.t.Log("newBad1")
- defer func() {
- if _panic := recover(); _panic == nil {
- sf.t.Fatalf("newBad1(): panic==nil")
- }
- }()
- var ctx context.Context
- sf.lCtx = NewLocalCtx(sf.owner, ctx)
- }
|