summaryrefslogtreecommitdiff
path: root/vendor/github.com/getlantern/context/context_test.go
blob: 9fb86f39bbaf7f2d7d38cd40a5b37f36f8d3097e (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package context

import (
	"sync"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestStack(t *testing.T) {
	cm := NewManager()
	_cm := cm.(*manager)
	// Put globals first
	cm.PutGlobal("a", -1) // This will get overriden in specific contexts
	cm.PutGlobal("ga", "i")
	cm.PutGlobalDynamic("gb", func() interface{} { return "ii" })
	cm.PutGlobalDynamic("gm", func() interface{} { return map[string]interface{}{"gm3": "iii", "gm4": "iv"} })

	// Use a Map as a Contextual
	var contextual = Map{
		"a":          0, // This will override whatever is in specific contexts
		"contextual": "special",
	}

	c := cm.Enter()
	c.Put("a", 1)
	penultimate := cm.Enter().
		Put("b", 2)
	c = cm.Enter().
		PutDynamic("c", func() interface{} { return 4 }).
		PutIfAbsent("d", 5).
		PutIfAbsent("a", 11)

	// Put something in the penultimate context and make sure it doesn't override
	// what's set in the ultimate context
	penultimate.Put("c", 3)

	var assertMutex sync.Mutex
	doAssertContents := func(expected Map, actual Map, scope string) {
		assertMutex.Lock()
		assert.Equal(t, expected, actual, scope)
		assertMutex.Unlock()
	}

	assertContents := func(expected Map) {
		doAssertContents(expected, cm.AsMap(nil, false), "AsMapwith(nil, false)")
		expected["ga"] = "i"
		expected["gb"] = "ii"
		expected["gm3"] = "iii"
		expected["gm4"] = "iv"
		_, exists := expected["a"]
		if !exists {
			expected["a"] = -1
		}
		doAssertContents(expected, cm.AsMap(nil, true), "AsMap(nil, true)")
		expected["a"] = 0
		expected["contextual"] = "special"
		doAssertContents(expected, cm.AsMap(contextual, true), "AsMapWith(contextual, true)")
		delete(expected, "ga")
		delete(expected, "gb")
		delete(expected, "gm3")
		delete(expected, "gm4")
		doAssertContents(expected, cm.AsMap(contextual, false), "AsMapWith(contextual, false)")
	}

	assertContents(Map{
		"a": 1,
		"b": 2,
		"c": 4,
		"d": 5,
	})

	var wg sync.WaitGroup
	wg.Add(1)
	cm.Go(func() {
		defer cm.Enter().Put("e", 6).Exit()
		assertContents(Map{
			"a": 1,
			"b": 2,
			"c": 4,
			"d": 5,
			"e": 6,
		})
		wg.Done()
	})
	wg.Wait()

	wg.Add(1)
	cm.Go(func() {
		// This goroutine doesn't Exit. Still, we shouldn't leak anything.
		wg.Done()
	})
	wg.Wait()

	assertContents(Map{
		"a": 1,
		"b": 2,
		"c": 4,
		"d": 5,
	})

	c.Exit()
	c = _cm.currentContext()
	assert.NotNil(t, c)
	assertContents(Map{
		"a": 1,
		"b": 2,
		"c": 3,
	})

	c.Exit()
	c = _cm.currentContext()
	assert.NotNil(t, c)
	assertContents(Map{
		"a": 1,
	})

	// Last exit
	c.Exit()
	assert.Nil(t, _cm.currentContext())
	assertContents(Map{})

	// Exit again, just for good measure
	c.Exit()
	assert.Nil(t, _cm.currentContext())
	assertContents(Map{})

	// Spawn a goroutine with no existing contexts
	wg.Add(1)
	cm.Go(func() {
		defer cm.Enter().Put("f", 7).Exit()
		assertContents(Map{
			"f": 7,
		})
		wg.Done()
	})
	wg.Wait()

	_cm.mxContexts.Lock()
	assert.Empty(t, _cm.contexts, "No contexts should be left")
	_cm.mxContexts.Unlock()
}

func BenchmarkPut(b *testing.B) {
	cm := NewManager()
	c := cm.Enter()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		c.Put("key", "value")
	}
}

func BenchmarkAsMap(b *testing.B) {
	cm := NewManager()
	cm.Enter().Put("a", 1).Put("b", 2)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		cm.AsMap(nil, true)
	}
}