-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers_test.go
97 lines (76 loc) · 2.52 KB
/
helpers_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
package plc
// test_helpers holds some mock datatypes and other useful contants that multiple test files
// may find helpful.
import (
"sync"
"time"
)
// Some shared constants acrossed reader/writer datatype tests.
const alphaCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const numCharset = "0123456789"
const alphanumCharset = alphaCharset + "_" + numCharset
const serialTestConcurrency = 1
const lowTestConcurrency = 10
const mediumTestConcurrency = 20
const highTestConcurrency = 30
const testWritePrecentage = 1
const heavyWritePerc = 50
// Stub ReadWriters
// latencyIntroducer simply delays processing a read or write request by some
// time interval. This is useful for artifically bounding throughput in
// concurrent tests.
type latencyIntroducer struct {
downstream ReadWriter
delay time.Duration
}
func newLatencyIntroducer(downstream ReadWriter, delay time.Duration) *latencyIntroducer {
return &latencyIntroducer{downstream, delay}
}
func (li *latencyIntroducer) ReadTag(name string, value interface{}) error {
time.Sleep(li.delay)
return li.downstream.ReadTag(name, value)
}
func (li *latencyIntroducer) WriteTag(name string, value interface{}) error {
time.Sleep(li.delay)
return li.downstream.WriteTag(name, value)
}
var _ = ReadWriter(&latencyIntroducer{})
// mockReadWriter is a dummy ReadWriter interface that unconditionally succeeds.
// We protect concurrenct accesses to the `state` map but _not_ the memory that
// the map's value points to, so it has to be the responsibility of the caller
// of ReadTag and WriteTag to ensure mutual exclusion on _their_ accesses.
// Typically, this will happen through a TagLocker wrapping this.
type mockReadWriter struct {
state map[string]*uint32 // State is read/written to for the benefit of the race detector during unit tests.
mtx sync.Mutex
}
var _ = ReadWriter(&TagLocker{})
func newMockReadWriter() *mockReadWriter {
return &mockReadWriter{
state: make(map[string]*uint32),
}
}
func (norw *mockReadWriter) ReadTag(name string, value interface{}) error {
//fmt.Fprintf(os.Stderr, "Downstream: Reading %v\n", name)
norw.mtx.Lock()
ptr, ok := norw.state[name]
if !ok {
ptr = new(uint32)
norw.state[name] = ptr
}
norw.mtx.Unlock()
value = *ptr
return nil
}
func (norw *mockReadWriter) WriteTag(name string, value interface{}) error {
//fmt.Fprintf(os.Stderr, "Downstream: Writing %v\n", name)
norw.mtx.Lock()
ptr, ok := norw.state[name]
if !ok {
ptr = new(uint32)
norw.state[name] = ptr
}
norw.mtx.Unlock()
*ptr++
return nil
}