Skip to content

Commit

Permalink
snet: remove goconvey
Browse files Browse the repository at this point in the history
Contributes scionproto#3016
  • Loading branch information
karampok committed Nov 8, 2019
1 parent 67956e1 commit 7691cc8
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 225 deletions.
1 change: 0 additions & 1 deletion go/lib/snet/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ go_test(
"//go/lib/spath/spathmeta:go_default_library",
"//go/lib/xtest:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_smartystreets_goconvey//convey:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
],
)
31 changes: 15 additions & 16 deletions go/lib/snet/addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"net"
"testing"

. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"

"github.com/scionproto/scion/go/lib/addr"
)
Expand All @@ -41,11 +41,11 @@ func Test_Addr_String(t *testing.T) {
{address: &Addr{IA: ia, Host: host4}, result: "1-ff00:0:320,[1.2.3.4]:10000"},
{address: &Addr{IA: ia, Host: host6}, result: "1-ff00:0:320,[2001::1]:20000"},
}
Convey("Method String", t, func() {
t.Run("Method String", func(t *testing.T) {
for _, test := range tests {
Convey(fmt.Sprintf("given address object %v", test.address), func() {
t.Run(fmt.Sprintf("given address object %v", test.address), func(t *testing.T) {
s := test.address.String()
SoMsg("String should match", s, ShouldResemble, test.result)
assert.Equal(t, s, test.result, "String should match")
})
}
})
Expand Down Expand Up @@ -105,19 +105,18 @@ func Test_AddrFromString(t *testing.T) {
host: "CS M (0x8002)",
},
}
Convey("Function AddrFromString", t, func() {
t.Run("Function AddrFromString", func(t *testing.T) {
for _, test := range tests {
Convey(fmt.Sprintf("given address %q", test.address), func() {
a, err := AddrFromString(test.address)
if test.isError {
SoMsg("error", err, ShouldNotBeNil)
} else {
SoMsg("error", err, ShouldBeNil)
SoMsg("ia", a.IA.String(), ShouldResemble, test.ia)
SoMsg("host", a.Host.L3.String(), ShouldResemble, test.host)
SoMsg("port", a.Host.L4, ShouldResemble, test.l4)
}
})
t.Log(fmt.Sprintf("given address %q", test.address))
a, err := AddrFromString(test.address)
if test.isError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, a.IA.String(), test.ia)
assert.Equal(t, a.Host.L3.String(), test.host)
assert.Equal(t, a.Host.L4, test.l4)
}
}
})
}
2 changes: 1 addition & 1 deletion go/lib/snet/internal/ctxmonitor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ go_test(
embed = [":go_default_library"],
deps = [
"//go/lib/xtest:go_default_library",
"@com_github_smartystreets_goconvey//convey:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
],
)
232 changes: 143 additions & 89 deletions go/lib/snet/internal/ctxmonitor/ctxmonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"testing"
"time"

. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"

"github.com/scionproto/scion/go/lib/xtest"
)
Expand All @@ -31,121 +31,175 @@ const (
)

func TestMonitor(t *testing.T) {
Convey("Given a monitor", t, func() {
t.Log("Given a monitor")
//m := NewMonitor()
t.Run("Add one context via Deadline, count is 1", func(t *testing.T) {
m := NewMonitor()
Convey("Add one context via Deadline, count is 1", func() {
m.WithDeadline(context.Background(), time.Now().Add(time.Millisecond))
So(m.Count(), ShouldEqual, 1)
})
Convey("Add one context via Timeout, count is 1", func() {
m.WithTimeout(context.Background(), time.Second)
So(m.Count(), ShouldEqual, 1)
})
Convey("Add two contexts, count is 2", func() {
m.WithTimeout(context.Background(), time.Second)
m.WithTimeout(context.Background(), time.Second)
So(m.Count(), ShouldEqual, 2)
})
Convey("Add one context, wait for it to expire, count is 0", func() {
ctx, cancelF := m.WithTimeout(context.Background(), time.Millisecond)
xtest.AssertReadReturnsBefore(t, ctx.Done(), maxWaitTime)
So(m.Count(), ShouldEqual, 0)
cancelF()
})
Convey("Add one context, cancel it, count is 0", func() {
_, cancelF := m.WithTimeout(context.Background(), time.Millisecond)
cancelF()
So(m.Count(), ShouldEqual, 0)
})
Convey("Add one context, wait for parent to expire, count is 0", func() {
parent, cancelF := context.WithTimeout(context.Background(), time.Microsecond)
m.WithTimeout(parent, time.Second)
xtest.AssertReadReturnsBefore(t, parent.Done(), maxWaitTime)
So(m.Count(), ShouldEqual, 0)
cancelF()
})
Convey("Add one context, cancel parent, count is 0", func() {
parent, parentCancelF := context.WithTimeout(context.Background(), time.Second)
m.WithTimeout(parent, time.Second)
parentCancelF()
So(m.Count(), ShouldEqual, 0)
})
Convey("Add one context, set deadline in the past, count is 0, ctx is Done", func() {
ctx, cancelF := m.WithTimeout(context.Background(), time.Second)
m.SetDeadline(time.Now().Add(-time.Second))
time.Sleep(sleepForCallback)
SoMsg("count", m.Count(), ShouldEqual, 0)
SoMsg("err", ctx.Err(), ShouldEqual, context.Canceled)
cancelF()
})
Convey("Add one context, set deadline in the future, count is 1, ctx is not Done", func() {
m.WithDeadline(context.Background(), time.Now().Add(time.Millisecond))
assert.Equal(t, m.Count(), 1)
})
t.Run("Add one context via Timeout, count is 1", func(t *testing.T) {
m := NewMonitor()
m.WithTimeout(context.Background(), time.Second)
assert.Equal(t, m.Count(), 1)
})
t.Run("Add two contexts, count is 2", func(t *testing.T) {
m := NewMonitor()
m.WithTimeout(context.Background(), time.Second)
m.WithTimeout(context.Background(), time.Second)
assert.Equal(t, m.Count(), 2)
})
t.Run("Add one context, wait for it to expire, count is 0", func(t *testing.T) {
m := NewMonitor()
ctx, cancelF := m.WithTimeout(context.Background(), time.Millisecond)
xtest.AssertReadReturnsBefore(t, ctx.Done(), maxWaitTime)
assert.Equal(t, m.Count(), 0)
cancelF()
})
t.Run("Add one context, cancel it, count is 0", func(t *testing.T) {
m := NewMonitor()
_, cancelF := m.WithTimeout(context.Background(), time.Millisecond)
cancelF()
assert.Equal(t, m.Count(), 0)
})
t.Run("Add one context, wait for parent to expire, count is 0", func(t *testing.T) {
m := NewMonitor()
parent, cancelF := context.WithTimeout(context.Background(), time.Microsecond)
m.WithTimeout(parent, time.Second)
xtest.AssertReadReturnsBefore(t, parent.Done(), maxWaitTime)
assert.Equal(t, m.Count(), 0)
cancelF()
})
t.Run("Add one context, cancel parent, count is 0", func(t *testing.T) {
m := NewMonitor()
parent, parentCancelF := context.WithTimeout(context.Background(), time.Second)
m.WithTimeout(parent, time.Second)
parentCancelF()
assert.Equal(t, m.Count(), 0)
})
t.Run("Add one context, set deadline in the past, count is 0, ctx is Done", func(t *testing.T) {
m := NewMonitor()
ctx, cancelF := m.WithTimeout(context.Background(), time.Second)
m.SetDeadline(time.Now().Add(-time.Second))
time.Sleep(sleepForCallback)
assert.Equal(t, m.Count(), 0, "count")
assert.Equal(t, ctx.Err(), context.Canceled, "err")
cancelF()
})
t.Run("Add one context, set deadline in the future, count is 1, ctx is not Done",
func(t *testing.T) {
m := NewMonitor()
ctx, cancelF := m.WithTimeout(context.Background(), time.Second)
m.SetDeadline(time.Now().Add(time.Second))
time.Sleep(sleepForCallback)
SoMsg("count", m.Count(), ShouldEqual, 1)
SoMsg("err", ctx.Err(), ShouldBeNil)
cancelF()
})
Convey("Set deadline to now, add context, ctx is immediately Done", func() {
m.SetDeadline(time.Now())
ctx, cancelF := m.WithTimeout(context.Background(), time.Second)
SoMsg("count", m.Count(), ShouldEqual, 0)
SoMsg("err", ctx.Err(), ShouldNotBeNil)
assert.Equal(t, m.Count(), 1, "count")
assert.NoError(t, ctx.Err())
cancelF()
})
Convey("Set deadline in future, add context with deadline after, ctx is not Done", func() {
t.Run("Set deadline to now, add context, ctx is immediately Done", func(t *testing.T) {
m := NewMonitor()
m.SetDeadline(time.Now())
ctx, cancelF := m.WithTimeout(context.Background(), time.Second)
assert.Equal(t, m.Count(), 0, "count")
assert.Error(t, ctx.Err())
cancelF()
})
t.Run("Set deadline in future, add context with deadline after, ctx is not Done",
func(t *testing.T) {
m := NewMonitor()
deadline := time.Now().Add(5 * time.Second)
m.SetDeadline(deadline)
ctx, cancelF := m.WithDeadline(context.Background(), deadline.Add(time.Second))
SoMsg("err", ctx.Err(), ShouldBeNil)
assert.NoError(t, ctx.Err())
cancelF()
})
})
}

func TestDeadlineRunner(t *testing.T) {
Convey("Given a deadline runner", t, func() {
t.Log("Given a deadline runner")
t.Run("If no deadline, function is not executed", func(t *testing.T) {
var runCount int64
assert.Equal(t, atomic.LoadInt64(&runCount), int64(0))
})
t.Run("If deadline in the past, function is executed", func(t *testing.T) {
var runCount int64
done := make(chan struct{})
r := NewDeadlineRunner(
func() {
atomic.AddInt64(&runCount, 1)
close(done)
})
Convey("If no deadline, function is not executed", func() {
So(atomic.LoadInt64(&runCount), ShouldEqual, 0)
})
Convey("If deadline in the past, function is executed", func() {
r.SetDeadline(time.Now().Add(-time.Second))
xtest.AssertReadReturnsBefore(t, done, time.Second)
So(atomic.LoadInt64(&runCount), ShouldEqual, 1)
})
Convey("If deadline in the future, function is not executed", func() {
r.SetDeadline(time.Now().Add(time.Second))
So(atomic.LoadInt64(&runCount), ShouldEqual, 0)
})
Convey("If deadline in the future and we wait, function is executed", func() {
r.SetDeadline(time.Now().Add(10 * time.Millisecond))
xtest.AssertReadReturnsBefore(t, done, time.Second)
So(atomic.LoadInt64(&runCount), ShouldEqual, 1)
})
Convey("If deadline in the future, and then extended, the first doesn't trigger", func() {

r.SetDeadline(time.Now().Add(-time.Second))
xtest.AssertReadReturnsBefore(t, done, time.Second)
assert.Equal(t, atomic.LoadInt64(&runCount), int64(1))
})
t.Run("If deadline in the future, function is not executed", func(t *testing.T) {
var runCount int64
done := make(chan struct{})
r := NewDeadlineRunner(
func() {
atomic.AddInt64(&runCount, 1)
close(done)
})

r.SetDeadline(time.Now().Add(time.Second))
assert.Equal(t, atomic.LoadInt64(&runCount), int64(0))
})
t.Run("If deadline in the future and we wait, function is executed", func(t *testing.T) {
var runCount int64
done := make(chan struct{})
r := NewDeadlineRunner(
func() {
atomic.AddInt64(&runCount, 1)
close(done)
})

r.SetDeadline(time.Now().Add(10 * time.Millisecond))
xtest.AssertReadReturnsBefore(t, done, time.Second)
assert.Equal(t, atomic.LoadInt64(&runCount), int64(1))
})
t.Run("If deadline in the future, and then extended, the first doesn't trigger",
func(t *testing.T) {
var runCount int64
done := make(chan struct{})
r := NewDeadlineRunner(
func() {
atomic.AddInt64(&runCount, 1)
close(done)
})

r.SetDeadline(time.Now().Add(10 * time.Millisecond))
r.SetDeadline(time.Now().Add(time.Second))
time.Sleep(20 * time.Millisecond)
So(atomic.LoadInt64(&runCount), ShouldEqual, 0)
})
Convey("If deadline in the future, and is then set in past, function is executed", func() {
assert.Equal(t, atomic.LoadInt64(&runCount), int64(0))
})
t.Run("If deadline in the future, and is then set in past, function is executed",
func(t *testing.T) {
var runCount int64
done := make(chan struct{})
r := NewDeadlineRunner(
func() {
atomic.AddInt64(&runCount, 1)
close(done)
})

r.SetDeadline(time.Now().Add(time.Second))
r.SetDeadline(time.Now().Add(-time.Second))
xtest.AssertReadReturnsBefore(t, done, time.Second)
So(atomic.LoadInt64(&runCount), ShouldEqual, 1)
})
Convey("Setting a deadline to 0 resets it, function is not executed", func() {
r.SetDeadline(time.Now().Add(10 * time.Millisecond))
r.SetDeadline(time.Time{})
time.Sleep(sleepForCallback)
So(atomic.LoadInt64(&runCount), ShouldEqual, 0)
assert.Equal(t, atomic.LoadInt64(&runCount), int64(1))
})
t.Run("Setting a deadline to 0 resets it, function is not executed", func(t *testing.T) {
var runCount int64
done := make(chan struct{})
r := NewDeadlineRunner(
func() {
atomic.AddInt64(&runCount, 1)
close(done)
})
r.SetDeadline(time.Now().Add(10 * time.Millisecond))
r.SetDeadline(time.Time{})
time.Sleep(sleepForCallback)
assert.Equal(t, atomic.LoadInt64(&runCount), int64(0))
})
}
Loading

0 comments on commit 7691cc8

Please sign in to comment.