aboutsummaryrefslogtreecommitdiff
path: root/internal/tracker/tracker_test.go
blob: c1161744ca64ee17d8c05fe6c4cd59bbff44d802 (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
package tracker

import (
	"testing"
	"time"

	"github.com/spf13/afero"
)

func TestTracker(t *testing.T) {
	fs := afero.NewMemMapFs()
	tracker, err := New(fs)
	if err != nil {
		t.Errorf("Must no err, got %v", err)
	}
	tid1, err := tracker.Add("activity 1", []string{}, []string{})
	if err != nil {
		t.Errorf("Must no err, got %v", err)
	}
	if tid1 != 1 {
		t.Errorf("Expected task id = 1, got %d", tid1)
	}
	tid2, err := tracker.Add("activity 2", []string{"tag1", "tag2"}, []string{"context1"})
	if err != nil {
		t.Errorf("Must no err, got %v", err)
	}
	if tid2 != 2 {
		t.Errorf("Expected task id = 2, got %d", tid2)
	}
	if err = tracker.Start(tid1, "work 1"); err != nil {
		t.Errorf("Must no err, got %v", err)
	}
	list := tracker.List(false)
	if len(list) != 1 {
		t.Errorf("List %v expected to be from 1 element", list)
	}
	list2 := tracker.List(true)
	if len(list2) != 2 {
		t.Errorf("List %v expected to be from 2 elements", list2)
	}
	<- time.After(2 * time.Second)
	if err := tracker.Stop(tid1); err != nil {
		t.Errorf("Must no err, got %v", err)
	}
	list3 := tracker.List(false)
	if len(list3) != 0 {
		t.Errorf("List %v expected to be from 0 element", list3)
	}
	list4 := tracker.List(true)
	for _, activity := range list4 {
		if activity.ID != tid1 {
			continue
		}
		if len(activity.Spans) != 1 {
			t.Errorf("List %v expected to be from 1 element", activity.Spans)
		}
		sp := activity.Spans[0]
		if sp.Stop == nil {
			t.Errorf("Span end time must be not empty")
		}
		if !sp.Stop.After(sp.Start) {
			t.Errorf("End span must be after start time")
		}
		if int(sp.Stop.Sub(sp.Start).Seconds()) != 2 {
			t.Errorf("difference between %v and %v must be 2 seconds, got %f", sp.Start, sp.Stop, sp.Stop.Sub(sp.Start).Seconds())
		}
	}
}