2009-04-19 21:17:27 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package exvar
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"exvar";
|
|
|
|
|
"fmt";
|
|
|
|
|
"testing";
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestSimpleCounter(t *testing.T) {
|
2009-04-20 22:38:14 -07:00
|
|
|
// Unknown exvar should be zero.
|
|
|
|
|
x := GetInt("requests");
|
|
|
|
|
if x != 0 {
|
2009-04-26 20:57:01 -07:00
|
|
|
t.Errorf("GetInt(nonexistent) = %v, want 0", x)
|
2009-04-19 21:17:27 -07:00
|
|
|
}
|
|
|
|
|
|
2009-04-20 22:38:14 -07:00
|
|
|
IncrementInt("requests", 1);
|
|
|
|
|
IncrementInt("requests", 3);
|
|
|
|
|
x = GetInt("requests");
|
|
|
|
|
if x != 4 {
|
2009-04-26 20:57:01 -07:00
|
|
|
t.Errorf("GetInt('requests') = %v, want 4", x)
|
2009-04-19 21:17:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out := String();
|
|
|
|
|
if out != "requests 4\n" {
|
|
|
|
|
t.Errorf("String() = \"%v\", want \"requests 4\n\"",
|
|
|
|
|
out);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-26 20:57:01 -07:00
|
|
|
func TestStringVar(t *testing.T) {
|
|
|
|
|
// Unknown exvar should be empty string.
|
|
|
|
|
if s := GetStr("name"); s != "" {
|
|
|
|
|
t.Errorf("GetStr(nonexistent) = %q, want ''", s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetStr("name", "Mike");
|
|
|
|
|
if s := GetStr("name"); s != "Mike" {
|
|
|
|
|
t.Errorf("GetStr('name') = %q, want 'Mike'", s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-20 22:38:14 -07:00
|
|
|
func TestMismatchedCounters(t *testing.T) {
|
|
|
|
|
// Make sure some vars exist.
|
|
|
|
|
GetInt("requests");
|
2009-04-21 16:50:09 -07:00
|
|
|
GetMapInt("colours", "red");
|
2009-04-26 20:57:01 -07:00
|
|
|
GetStr("name");
|
2009-04-20 22:38:14 -07:00
|
|
|
|
|
|
|
|
IncrementInt("colours", 1);
|
|
|
|
|
if x := GetInt("x-mismatched-int"); x != 1 {
|
|
|
|
|
t.Errorf("GetInt('x-mismatched-int') = %v, want 1", x)
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-21 16:50:09 -07:00
|
|
|
IncrementMapInt("requests", "orange", 1);
|
|
|
|
|
if x := GetMapInt("x-mismatched-map", "orange"); x != 1 {
|
2009-04-26 20:57:01 -07:00
|
|
|
t.Errorf("GetMapInt('x-mismatched-map', 'orange') = %v, want 1", x)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetStr("requests", "apple");
|
|
|
|
|
if s := GetStr("x-mismatched-str"); s != "apple" {
|
|
|
|
|
t.Errorf("GetStr('x-mismatched-str') = %q, want 'apple'", s)
|
2009-04-20 22:38:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMapCounter(t *testing.T) {
|
|
|
|
|
// Unknown exvar should be zero.
|
2009-04-21 16:50:09 -07:00
|
|
|
if x := GetMapInt("colours", "red"); x != 0 {
|
|
|
|
|
t.Errorf("GetMapInt(non, existent) = %v, want 0", x)
|
2009-04-20 22:38:14 -07:00
|
|
|
}
|
|
|
|
|
|
2009-04-21 16:50:09 -07:00
|
|
|
IncrementMapInt("colours", "red", 1);
|
|
|
|
|
IncrementMapInt("colours", "red", 2);
|
|
|
|
|
IncrementMapInt("colours", "blue", 4);
|
|
|
|
|
if x := GetMapInt("colours", "red"); x != 3 {
|
|
|
|
|
t.Errorf("GetMapInt('colours', 'red') = %v, want 3", x)
|
2009-04-20 22:38:14 -07:00
|
|
|
}
|
2009-04-21 16:50:09 -07:00
|
|
|
if x := GetMapInt("colours", "blue"); x != 4 {
|
|
|
|
|
t.Errorf("GetMapInt('colours', 'blue') = %v, want 4", x)
|
2009-04-20 22:38:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO(dsymonds): Test String()
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-19 21:17:27 -07:00
|
|
|
func hammer(name string, total int, done chan <- int) {
|
|
|
|
|
for i := 0; i < total; i++ {
|
2009-04-20 22:38:14 -07:00
|
|
|
IncrementInt(name, 1)
|
2009-04-19 21:17:27 -07:00
|
|
|
}
|
|
|
|
|
done <- 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHammer(t *testing.T) {
|
2009-04-20 22:38:14 -07:00
|
|
|
SetInt("hammer-times", 0);
|
2009-04-19 21:17:27 -07:00
|
|
|
sync := make(chan int);
|
|
|
|
|
hammer_times := int(1e5);
|
|
|
|
|
go hammer("hammer-times", hammer_times, sync);
|
|
|
|
|
go hammer("hammer-times", hammer_times, sync);
|
|
|
|
|
<-sync;
|
|
|
|
|
<-sync;
|
2009-04-20 22:38:14 -07:00
|
|
|
if final := GetInt("hammer-times"); final != 2 * hammer_times {
|
2009-04-19 21:17:27 -07:00
|
|
|
t.Errorf("hammer-times = %v, want %v", final, 2 * hammer_times)
|
|
|
|
|
}
|
|
|
|
|
}
|