-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc_test.go
228 lines (212 loc) · 5.54 KB
/
misc_test.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2018-2022 YottaDB LLC and/or its subsidiaries. //
// All rights reserved. //
// //
// This source code contains the intellectual property //
// of its copyright holder(s), and is made available //
// under a license. If you do not know the terms of //
// the license, please stop and do not read further. //
// //
//////////////////////////////////////////////////////////////////
package yottadb_test
import (
"errors"
"github.com/stretchr/testify/assert"
"lang.yottadb.com/go/yottadb"
. "lang.yottadb.com/go/yottadb/internal/test_helpers"
"sync"
"testing"
"time"
)
// Allow for 30% difference between Expected and Actual in timed tests.
// In testing, we have seen a difference up to 23% show up some times in the "miscGoTimersHelper()" timed test.
// Hence the current choice of 30% in the "allowedErrorPct" variable.
var allowedErrorPct float64 = 0.30
func TestMiscIsLittleEndian(t *testing.T) {
// For now, we only see this getting run on LittleEndian machines, so just verify
// true
assert.True(t, yottadb.IsLittleEndian())
}
func TestMiscAssertnoerror(t *testing.T) {
err := errors.New("This is a test error")
defer func() {
r := recover()
assert.NotNil(t, r)
}()
panic(err)
}
func miscGoTimersHelper(t *testing.T, wg *sync.WaitGroup, loops int) {
wg.Add(1)
go func() {
for i := 0; i < loops; i++ {
start := time.Now()
r, err := yottadb.CallMT(yottadb.NOTTP, nil, 0, "TestMGoTimers")
assert.Nil(t, err)
elapsed := time.Since(start)
assert.InEpsilon(t, 1, elapsed.Seconds(), allowedErrorPct)
assert.Equal(t, "", r)
}
wg.Done()
}()
}
func TestMiscGoTimers(t *testing.T) {
// Verify that Go timers do not interfere with YDB timers; kick off a thread
// which invokes a M routine that sleeps 100ms 10 times, kick off 10 go routines
// which sleep for 100ms
var wg sync.WaitGroup
SkipTimedTests(t)
SkipCITests(t)
miscGoTimersHelper(t, &wg, 2)
sleepDuration, e := time.ParseDuration("100ms")
assert.Nil(t, e)
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
for j := 0; j < 20; j++ {
start := time.Now()
time.Sleep(sleepDuration)
elapsed := time.Since(start)
assert.InEpsilon(t, .1, elapsed.Seconds(), allowedErrorPct)
}
wg.Done()
}()
}
wg.Wait()
}
func TestMiscGoSelectWithYdbTimers(t *testing.T) {
// Verify Go channels do not interfere with YDB timers
var wg sync.WaitGroup
SkipTimedTests(t)
SkipCITests(t)
miscGoTimersHelper(t, &wg, 10)
// Spawn off consume-producer routines, sending 100 messages at 10ms intervals (10s test)
sleepDuration, e := time.ParseDuration("10ms")
recvCount := 0
ch := make(chan int)
assert.Nil(t, e)
wg.Add(1)
go func() {
for i := 0; i < 100; i++ {
ch <- i
time.Sleep(sleepDuration)
}
close(ch)
wg.Done()
}()
wg.Add(1)
go func() {
for range ch {
recvCount++
}
wg.Done()
}()
wg.Wait()
assert.Equal(t, 100, recvCount)
}
func TestMiscGoSelectWithYdbTimers2(t *testing.T) {
// Verify Go select/channels do not interfere with YDB timers
var wg sync.WaitGroup
SkipTimedTests(t)
SkipCITests(t)
miscGoTimersHelper(t, &wg, 10)
// Spawn off consume-producer routines, sending 100 messages at 10ms intervals (10s test)
sleepDuration, e := time.ParseDuration("10ms")
recvCount := 0
ch := make(chan int)
assert.Nil(t, e)
wg.Add(1)
go func() {
for i := 0; i < 100; i++ {
ch <- i
time.Sleep(sleepDuration)
}
ch <- -1
close(ch)
wg.Done()
}()
wg.Add(1)
go func() {
for true {
done := false
select {
case x := <-ch:
if x == -1 {
done = true
break
}
recvCount++
default:
continue
}
if done {
break
}
}
wg.Done()
}()
wg.Wait()
assert.Equal(t, 100, recvCount)
}
func TestMiscGoSelectWithYdbTimers3(t *testing.T) {
// Verify Go select/channels/time.After does not interfere with YDB timers
var wg sync.WaitGroup
SkipTimedTests(t)
SkipCITests(t)
miscGoTimersHelper(t, &wg, 10)
// Spawn off consume-producer routines, sending 100 messages at 10ms intervals (10s test)
sleepDuration, e := time.ParseDuration("10ms")
recvCount := 0
ch := make(chan int)
assert.Nil(t, e)
wg.Add(1)
go func() {
for i := 0; i < 100; i++ {
ch <- i
time.Sleep(sleepDuration)
}
ch <- -1
close(ch)
wg.Done()
}()
wg.Add(1)
go func() {
for true {
done := false
select {
case x := <-ch:
if x == -1 {
done = true
break
}
recvCount++
case <-time.After(5 * time.Millisecond):
continue
}
if done {
break
}
}
wg.Done()
}()
wg.Wait()
assert.Equal(t, 100, recvCount)
}
// Test the 4 timer global variable timer values to make sure they can be accessed and updated.
func testTimerParameter(t *testing.T, waitValue *time.Duration, defaultValue time.Duration) {
assert.Equal(t, *waitValue, defaultValue) // Expect it to be its default value initially
*waitValue++ // Bump it by a second
assert.Equal(t, *waitValue, defaultValue+1) // Verify the update worked
}
func TestMaximumNormalExitWait(t *testing.T) {
testTimerParameter(t, &yottadb.MaximumNormalExitWait, yottadb.DefaultMaximumNormalExitWait)
}
func TestMaximumPanicExitWait(t *testing.T) {
testTimerParameter(t, &yottadb.MaximumPanicExitWait, yottadb.DefaultMaximumPanicExitWait)
}
func TestMaximumSigShutDownWait(t *testing.T) {
testTimerParameter(t, &yottadb.MaximumSigShutDownWait, yottadb.DefaultMaximumSigShutDownWait)
}
func TestMaximumSigAckWait(t *testing.T) {
testTimerParameter(t, &yottadb.MaximumSigAckWait, yottadb.DefaultMaximumSigAckWait)
}