-
Notifications
You must be signed in to change notification settings - Fork 1
/
leaderboard_test.go
55 lines (43 loc) · 942 Bytes
/
leaderboard_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"testing"
"github.com/go-playground/assert/v2"
)
func initLeaderBoard() *leaderBoard {
l := newLeaderBoard()
for i := 0; i < 10; i++ {
for j := 0; j < 10; j++ {
l.push(fmt.Sprint(i), fmt.Sprint(j))
}
}
return l
}
func TestPush(t *testing.T) {
l := initLeaderBoard()
for i := 0; i < 10; i++ {
board := l.games[fmt.Sprint(i)]
for j := 0; j < 3; j++ {
assert.Equal(t, board[j], fmt.Sprint(9-j))
}
}
}
func TestFlush(t *testing.T) {
l := initLeaderBoard()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var msg message
err := decoder.Decode(&msg)
if err != nil {
log.Fatalln("Invalid input")
}
assert.Equal(t, msg.Items, [3]string{"9", "8", "7"})
})
go http.ListenAndServe(":8080", nil)
for i := 0; i < 10; i++ {
l.flush("http://localhost:8080", fmt.Sprint(i))
}
}