p2p/trust: Fix nil pointer error on TrustMetric Copy() (#1819)

This commit is contained in:
Ricardo Domingos 2018-06-27 06:37:30 +02:00 committed by Anton Kaliaev
parent 8412b75b10
commit e6abdb8b9d
2 changed files with 11 additions and 2 deletions

View File

@ -256,12 +256,13 @@ func (tm *TrustMetric) SetTicker(ticker MetricTicker) {
// Copy returns a new trust metric with members containing the same values
func (tm *TrustMetric) Copy() *TrustMetric {
tm.mtx.Lock()
defer tm.mtx.Unlock()
if tm == nil {
return nil
}
tm.mtx.Lock()
defer tm.mtx.Unlock()
return &TrustMetric{
proportionalWeight: tm.proportionalWeight,
integralWeight: tm.integralWeight,

View File

@ -56,6 +56,14 @@ func TestTrustMetricConfig(t *testing.T) {
tm.Wait()
}
func TestTrustMetricCopyNilPointer(t *testing.T) {
var tm *TrustMetric
ctm := tm.Copy()
assert.Nil(t, ctm)
}
// XXX: This test fails non-deterministically
func _TestTrustMetricStopPause(t *testing.T) {
// The TestTicker will provide manual control over