File: contextdispatcher_test.go

package info (click to toggle)
golang-github-bep-helpers 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 148 kB
  • sloc: makefile: 2
file content (31 lines) | stat: -rw-r--r-- 820 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Copyright 2024 Bjørn Erik Pedersen
// SPDX-License-Identifier: MIT

package contexthelpers

import (
	"context"
	"testing"

	qt "github.com/frankban/quicktest"
)

func TestContextDispatcher(t *testing.T) {
	c := qt.New(t)

	ctx := context.Background()
	dispatcher1 := NewContextDispatcher[string](1)
	dispatcher2 := NewContextDispatcher[string](2)
	ctx = dispatcher1.Set(ctx, "testValue")
	c.Assert(dispatcher1.Get(ctx), qt.Equals, "testValue")
	c.Assert(dispatcher2.Get(ctx), qt.Equals, "")
	c.Assert(dispatcher1.Get(context.Background()), qt.Equals, "")

	value, found := dispatcher1.Lookup(ctx)
	c.Assert(found, qt.IsTrue)
	c.Assert(value, qt.Equals, "testValue")
	_, found = dispatcher1.Lookup(context.Background())
	c.Assert(found, qt.IsFalse)
	_, found = dispatcher2.Lookup(ctx)
	c.Assert(found, qt.IsFalse)
}