-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from dwelch0/metric-proxy-rw-lock
utilize rwlock to address race condition - add tests
- Loading branch information
Showing
2 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package pkg | ||
|
||
import ( | ||
"math" | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestGetMetricById(t *testing.T) { | ||
type args struct { | ||
mp *MetricProxy | ||
key string | ||
} | ||
now := time.Now() | ||
tests := []struct { | ||
name string | ||
args args | ||
want *MetricProxyItem | ||
}{ | ||
{ | ||
name: "Attempt retrieving value by providing valid id (key exists)", | ||
args: args{ | ||
mp: &MetricProxy{ | ||
metrics: map[string]*MetricProxyItem{ | ||
"valid": &MetricProxyItem{ | ||
value: "value", | ||
ttl: math.MaxInt32, | ||
creationTime: now, | ||
}, | ||
}, | ||
}, | ||
key: "valid", | ||
}, | ||
want: &MetricProxyItem{ | ||
value: "value", | ||
ttl: math.MaxInt32, | ||
creationTime: now, | ||
}, | ||
}, | ||
{ | ||
name: "Attempt retrieving value by providing valid id but ttl expired", | ||
args: args{ | ||
mp: &MetricProxy{ | ||
metrics: map[string]*MetricProxyItem{ | ||
"expired": &MetricProxyItem{ | ||
value: 100, | ||
ttl: 1, | ||
creationTime: now, | ||
}, | ||
}, | ||
}, | ||
key: "expired", | ||
}, | ||
want: nil, | ||
}, | ||
} | ||
|
||
time.Sleep(2 * time.Second) //ensure ttl for second test expires | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got, _ := tt.args.mp.GetMetricById(tt.args.key); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetMetricById() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestStoreMetricById(t *testing.T) { | ||
type args struct { | ||
mp *MetricProxy | ||
key string | ||
value interface{} | ||
ttl int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *MetricProxyItem | ||
}{ | ||
{ | ||
name: "Attempt storing new metric by id", | ||
args: args{ | ||
mp: NewMetricProxy(), | ||
key: "new", | ||
value: 1, | ||
ttl: math.MaxInt32, | ||
}, | ||
want: &MetricProxyItem{ | ||
value: 1, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.args.mp.StoreMetricById(tt.args.key, tt.args.value, tt.args.ttl) | ||
if got, err := tt.args.mp.GetMetricById(tt.args.key); got.value != tt.want.value || err != nil { | ||
t.Errorf("StoreMetricById() = %v, want %v", got.value, tt.want.value) | ||
} | ||
}) | ||
} | ||
} |