Guard request counter in ipfs mock with mutex

Fixes several data races across multiple tests.
This commit is contained in:
Ian Davis 2021-07-28 13:51:03 +01:00
parent cb4023855c
commit cda59b656e
No known key found for this signature in database
GPG Key ID: F3B290C645FE1783

View File

@ -40,9 +40,11 @@ type IpfsMock struct {
Port int
pinMap state.State
BlockStore map[string][]byte
reqCounts map[string]int
reqCounter chan string
reqCountsMux sync.Mutex // guards access to reqCounts
reqCounts map[string]int
closeMux sync.Mutex
closed bool
}
@ -142,18 +144,20 @@ func NewIpfsMock(t *testing.T) *IpfsMock {
m.Port = i
m.Addr = h[0]
return m
}
func (m *IpfsMock) countRequests() {
for str := range m.reqCounter {
m.reqCountsMux.Lock()
m.reqCounts[str]++
m.reqCountsMux.Unlock()
}
}
// GetCount allows to get the number of times and endpoint was called.
// Do not use concurrently to requests happening.
func (m *IpfsMock) GetCount(path string) int {
m.reqCountsMux.Lock()
defer m.reqCountsMux.Unlock()
return m.reqCounts[path]
}