| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package local_ctx
- import (
- "context"
- "testing"
- )
- type tester struct {
- t *testing.T
- ctx *LocalCtx
- }
- func TestLocalCtx(t *testing.T) {
- sf := &tester{
- t: t,
- }
- sf.new()
- sf.set()
- sf.get()
- sf.del()
- sf.done()
- }
- // Ожидает отмены контекста
- func (sf *tester) done() {
- sf.t.Log("done")
- _ = sf.ctx.Size()
- go sf.ctx.Cancel()
- sf.ctx.Done()
- }
- // Удаляет несуществующий ключ из локального контекста
- func (sf *tester) del() {
- sf.t.Log("del")
- sf.ctx.Del("123")
- sf.ctx.Del("count")
- }
- // Возвращает хранимое значение
- func (sf *tester) get() {
- sf.t.Log("get")
- val := sf.ctx.Get("count")
- count := val.Val().(int)
- if count == 15 {
- return
- }
- }
- // Устанавливает значение
- func (sf *tester) set() {
- sf.t.Log("set")
- sf.ctx.Set("count", 5, "test_val")
- sf.ctx.Set("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.ctx = NewLocalCtx(ctx).(*LocalCtx)
- _ = sf.ctx.Log()
- if lst := sf.ctx.SortedList(); lst == nil {
- sf.t.Fatalf("newGood1(): lst==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.ctx = NewLocalCtx(ctx).(*LocalCtx)
- }
|