@@ -26,7 +26,7 @@ func TestTable(t *testing.T) {
26
26
27
27
config := snmp.ClientConfig {
28
28
Version : 2 ,
29
- Timeout : internal.Duration {Duration : 5 * time .Second }, // doesn 't work with 0 timeout
29
+ Timeout : internal.Duration {Duration : 5 * time .Second }, // Doesn 't work with 0 timeout
30
30
}
31
31
gs , err := snmp .NewWrapper (config )
32
32
require .NoError (t , err )
@@ -36,7 +36,7 @@ func TestTable(t *testing.T) {
36
36
err = gs .Connect ()
37
37
require .NoError (t , err )
38
38
39
- //could use ifIndex but oid index is always the same
39
+ // Could use ifIndex but oid index is always the same
40
40
m , err := buildMap (gs , tab , "ifDescr" )
41
41
require .NoError (t , err )
42
42
require .NotEmpty (t , m )
@@ -53,7 +53,7 @@ func TestIfName(t *testing.T) {
53
53
CacheSize : 1000 ,
54
54
ClientConfig : snmp.ClientConfig {
55
55
Version : 2 ,
56
- Timeout : internal.Duration {Duration : 5 * time .Second }, // doesn 't work with 0 timeout
56
+ Timeout : internal.Duration {Duration : 5 * time .Second }, // Doesn 't work with 0 timeout
57
57
},
58
58
}
59
59
err := d .Init ()
@@ -93,65 +93,52 @@ func TestIfName(t *testing.T) {
93
93
94
94
func TestGetMap (t * testing.T ) {
95
95
d := IfName {
96
- SourceTag : "ifIndex" ,
97
- DestTag : "ifName" ,
98
- AgentTag : "agent" ,
99
96
CacheSize : 1000 ,
100
- ClientConfig : snmp.ClientConfig {
101
- Version : 2 ,
102
- Timeout : internal.Duration {Duration : 5 * time .Second }, // doesn't work with 0 timeout
103
- },
104
- CacheTTL : config .Duration (10 * time .Second ),
97
+ CacheTTL : config .Duration (10 * time .Second ),
105
98
}
106
99
107
- // This test mocks the snmp transaction so don't run net-snmp
108
- // commands to look up table names.
100
+ // Don't run net-snmp commands to look up table names.
109
101
d .makeTable = func (agent string ) (* si.Table , error ) {
110
102
return & si.Table {}, nil
111
103
}
112
104
err := d .Init ()
113
105
require .NoError (t , err )
114
106
115
- // Request the same agent multiple times in goroutines. The first
116
- // request should make the mocked remote call and the others
117
- // should block until the response is cached, then return the
118
- // cached response.
119
-
120
107
expected := nameMap {
121
108
1 : "ifname1" ,
122
109
2 : "ifname2" ,
123
110
}
124
111
125
- var wgRemote sync.WaitGroup
126
112
var remoteCalls int32
127
113
128
- wgRemote . Add ( 1 )
114
+ // Mock the snmp transaction
129
115
d .getMapRemote = func (agent string ) (nameMap , error ) {
130
116
atomic .AddInt32 (& remoteCalls , 1 )
131
- wgRemote .Wait () //don't return until all requests are made
132
117
return expected , nil
133
118
}
119
+ m , age , err := d .getMap ("agent" )
120
+ require .NoError (t , err )
121
+ require .Zero (t , age ) // Age is zero when map comes from getMapRemote
122
+ require .Equal (t , expected , m )
134
123
135
- const thMax = 3
136
- var wgReq sync. WaitGroup
124
+ // Remote call should happen the first time getMap runs
125
+ require . Equal ( t , int32 ( 1 ), remoteCalls )
137
126
127
+ var wg sync.WaitGroup
128
+ const thMax = 3
138
129
for th := 0 ; th < thMax ; th ++ {
139
- wgReq .Add (1 )
130
+ wg .Add (1 )
140
131
go func () {
141
- defer wgReq .Done ()
142
- m , _ , err := d .getMap ("agent" )
132
+ defer wg .Done ()
133
+ m , age , err := d .getMap ("agent" )
143
134
require .NoError (t , err )
135
+ require .NotZero (t , age ) // Age is nonzero when map comes from cache
144
136
require .Equal (t , expected , m )
145
137
}()
146
138
}
147
139
148
- //signal mocked remote call to finish
149
- wgRemote .Done ()
150
-
151
- //wait for requests to finish
152
- wgReq .Wait ()
140
+ wg .Wait ()
153
141
154
- //remote call should only happen once
142
+ // Remote call should not happen subsequent times getMap runs
155
143
require .Equal (t , int32 (1 ), remoteCalls )
156
-
157
144
}
0 commit comments