123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package dashboard
- import (
- "bufio"
- "os"
- "sync"
- "testing"
- "time"
- //"fmt"
- "github.com/flashmob/go-guerrilla/log"
- "regexp"
- "strings"
- )
- func TestRunStop(t *testing.T) {
- config := &Config{
- Enabled: true,
- ListenInterface: ":8082",
- TickInterval: "5s",
- MaxWindow: "24h",
- RankingUpdateInterval: "6h",
- }
- var wg sync.WaitGroup
- wg.Add(1)
- go func() {
- Run(config)
- wg.Done()
- }()
- // give Run some time to start
- time.Sleep(time.Second)
- Stop()
- // Wait for Run() to exit
- wg.Wait()
- }
- // Test if starting with a bad interface address
- func TestRunStopBadAddress(t *testing.T) {
- config := &Config{
- Enabled: true,
- ListenInterface: "1.1.1.1:0",
- TickInterval: "5s",
- MaxWindow: "24h",
- RankingUpdateInterval: "6h",
- }
- var wg sync.WaitGroup
- wg.Add(1)
- go func() {
- Run(config)
- wg.Done()
- }()
- time.Sleep(time.Second * 2)
- Stop()
- // Wait for Run() to exit
- wg.Wait()
- }
- // Run a simulation from an already captured log
- func TestSimulationRun(t *testing.T) {
- config := &Config{
- Enabled: true,
- ListenInterface: ":8082",
- TickInterval: "1s",
- MaxWindow: "24h",
- RankingUpdateInterval: "6h",
- }
- var wg sync.WaitGroup
- wg.Add(1)
- go func() {
- Run(config)
- wg.Done()
- }()
- // give Run some time to start
- time.Sleep(time.Second)
- file, err := os.Open("simulation.log")
- if err != nil {
- panic(err.Error())
- }
- defer file.Close()
- reader := bufio.NewReader(file)
- scanner := bufio.NewScanner(reader)
- scanner.Split(bufio.ScanLines)
- l, _ := log.GetLogger("stderr", "info")
- l.AddHook(LogHook)
- // match with quotes or without, ie. time="..." or level=
- r := regexp.MustCompile(`(.+?)=("[^"]*"|\S*)\s*`)
- simStart := time.Now()
- var start time.Time
- for scanner.Scan() {
- fields := map[string]interface{}{}
- line := scanner.Text()
- items := r.FindAllString(line, -1)
- msg := ""
- var logElapsed time.Duration
- for i := range items {
- key, val := parseItem(items[i])
- //fmt.Println(key, val)
- if key != "time" && key != "level" && key != "msg" {
- fields[key] = val
- }
- if key == "msg" {
- msg = val
- }
- if key == "time" {
- tv, err := time.Parse(time.RFC3339, val)
- if err != nil {
- t.Error("invalid time", tv)
- }
- if start.IsZero() {
- start = tv
- }
- fields["start"] = start
- logElapsed = tv.Sub(start)
- }
- }
- diff := time.Now().Sub(simStart) - logElapsed
- time.Sleep(diff) // wait so that we don't go too fast
- simStart = simStart.Add(diff) // catch up
- l.WithFields(fields).Info(msg)
- }
- Stop()
- // Wait for Run() to exit
- wg.Wait()
- }
- // parseItem parses a log item, eg time="2017-03-24T11:55:44+11:00" will be:
- // key = time and val will be 2017-03-24T11:55:44+11:00
- func parseItem(item string) (key string, val string) {
- arr := strings.Split(item, "=")
- if len(arr) == 2 {
- key = arr[0]
- if arr[1][0:1] == "\"" {
- pos := len(arr[1]) - 2
- val = arr[1][1:pos]
- } else {
- val = arr[1]
- }
- }
- val = strings.TrimSpace(val)
- return
- }
|