-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
114 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package awsping | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDuration(t *testing.T) { | ||
input := time.Duration(1 * time.Second) | ||
want := 1000.0 | ||
got := Duration2ms(input) | ||
if got != want { | ||
t.Errorf("Duration was incorrect, got: %f, want: %f.", got, want) | ||
} | ||
} | ||
|
||
func TestRandomString(t *testing.T) { | ||
tests := []struct { | ||
n int | ||
res string | ||
}{ | ||
{1, ""}, | ||
{5, ""}, | ||
{10, ""}, | ||
} | ||
|
||
for idx, test := range tests { | ||
test.res = mkRandoString(test.n) | ||
if len(test.res) != test.n { | ||
t.Errorf("Try %d: n=%d, got: %s (len=%d), want: %d.", | ||
idx, test.n, test.res, len(test.res), test.n) | ||
} | ||
} | ||
} | ||
|
||
func TestOutputShowOnlyRegions(t *testing.T) { | ||
var b bytes.Buffer | ||
|
||
lo := NewOutput(ShowOnlyRegions, 0) | ||
lo.w = &b | ||
|
||
regions := GetRegions("sqs")[:2] | ||
lo.Show(®ions) | ||
|
||
got := b.String() | ||
want := "us-east-1 US-East (N. Virginia)\n" + | ||
"us-east-2 US-East (Ohio) \n" | ||
if got != want { | ||
t.Errorf("Show: got=%q\nwant=%q", got, want) | ||
} | ||
} | ||
|
||
func TestOutputShow0(t *testing.T) { | ||
var b bytes.Buffer | ||
|
||
lo := NewOutput(0, 0) | ||
lo.w = &b | ||
|
||
regions := GetRegions("sqs")[:2] | ||
regions[0].Latencies = []time.Duration{15 * time.Millisecond} | ||
regions[1].Latencies = []time.Duration{25 * time.Millisecond} | ||
|
||
lo.Show(®ions) | ||
|
||
want := "US-East (N. Virginia) 15.00 ms\n" + | ||
"US-East (Ohio) 25.00 ms\n" | ||
got := b.String() | ||
if got != want { | ||
t.Errorf("Show0 failed:\ngot=%q\nwant=%q", got, want) | ||
} | ||
} | ||
|
||
func TestOutputShow1(t *testing.T) { | ||
var b bytes.Buffer | ||
|
||
lo := NewOutput(1, 0) | ||
lo.w = &b | ||
|
||
regions := GetRegions("sqs")[:2] | ||
regions[0].Latencies = []time.Duration{15 * time.Millisecond} | ||
regions[1].Latencies = []time.Duration{25 * time.Millisecond} | ||
|
||
lo.Show(®ions) | ||
|
||
got := b.String() | ||
want := " Code Region Latency\n" + | ||
" 0 us-east-1 US-East (N. Virginia) 15.00 ms\n" + | ||
" 1 us-east-2 US-East (Ohio) 25.00 ms\n" | ||
if got != want { | ||
t.Errorf("Show1 failed:\ngot=%q\nwant=%q", got, want) | ||
} | ||
} | ||
|
||
func TestOutputShow2(t *testing.T) { | ||
var b bytes.Buffer | ||
|
||
lo := NewOutput(2, 2) | ||
lo.w = &b | ||
|
||
regions := GetRegions("sqs")[:2] | ||
regions[0].Latencies = []time.Duration{15 * time.Millisecond, 17 * time.Millisecond} | ||
regions[1].Latencies = []time.Duration{25 * time.Millisecond, 26 * time.Millisecond} | ||
|
||
lo.Show(®ions) | ||
|
||
got := b.String() | ||
want := " Code Region Try #1 Try #2 Avg Latency\n" + | ||
" 0 us-east-1 US-East (N. Virginia) 15.00 ms 17.00 ms 16.00 ms\n" + | ||
" 1 us-east-2 US-East (Ohio) 25.00 ms 26.00 ms 25.50 ms\n" | ||
if got != want { | ||
t.Errorf("Show2 failed:\ngot=%q\nwant=%q", got, want) | ||
} | ||
} |