mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-17 14:37:02 +00:00
testlib: Add comments and unexport unnecessary structs
This patch contains some readability improvements to testlib: it adds/reformats some comments for exported functions for consistency, and unexports some structs that are not used outside the library.
This commit is contained in:
@@ -67,6 +67,7 @@ func GetFreePort() string {
|
|||||||
return l.Addr().String()
|
return l.Addr().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WaitFor f to return true (returns true), or d to pass (returns false).
|
||||||
func WaitFor(f func() bool, d time.Duration) bool {
|
func WaitFor(f func() bool, d time.Duration) bool {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
for time.Since(start) < d {
|
for time.Since(start) < d {
|
||||||
@@ -78,23 +79,24 @@ func WaitFor(f func() bool, d time.Duration) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeliverRequest struct {
|
type deliverRequest struct {
|
||||||
From string
|
From string
|
||||||
To string
|
To string
|
||||||
Data []byte
|
Data []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// Courier for test purposes. Never fails, and always remembers everything.
|
// TestCourier never fails, and always remembers everything.
|
||||||
type TestCourier struct {
|
type TestCourier struct {
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
Requests []*DeliverRequest
|
Requests []*deliverRequest
|
||||||
ReqFor map[string]*DeliverRequest
|
ReqFor map[string]*deliverRequest
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deliver the given mail (saving it in tc.Requests).
|
||||||
func (tc *TestCourier) Deliver(from string, to string, data []byte) (error, bool) {
|
func (tc *TestCourier) Deliver(from string, to string, data []byte) (error, bool) {
|
||||||
defer tc.wg.Done()
|
defer tc.wg.Done()
|
||||||
dr := &DeliverRequest{from, to, data}
|
dr := &deliverRequest{from, to, data}
|
||||||
tc.Lock()
|
tc.Lock()
|
||||||
tc.Requests = append(tc.Requests, dr)
|
tc.Requests = append(tc.Requests, dr)
|
||||||
tc.ReqFor[to] = dr
|
tc.ReqFor[to] = dr
|
||||||
@@ -102,10 +104,12 @@ func (tc *TestCourier) Deliver(from string, to string, data []byte) (error, bool
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Expect i mails to be delivered.
|
||||||
func (tc *TestCourier) Expect(i int) {
|
func (tc *TestCourier) Expect(i int) {
|
||||||
tc.wg.Add(i)
|
tc.wg.Add(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wait until all mails have been delivered.
|
||||||
func (tc *TestCourier) Wait() {
|
func (tc *TestCourier) Wait() {
|
||||||
tc.wg.Wait()
|
tc.wg.Wait()
|
||||||
}
|
}
|
||||||
@@ -113,7 +117,7 @@ func (tc *TestCourier) Wait() {
|
|||||||
// NewTestCourier returns a new, empty TestCourier instance.
|
// NewTestCourier returns a new, empty TestCourier instance.
|
||||||
func NewTestCourier() *TestCourier {
|
func NewTestCourier() *TestCourier {
|
||||||
return &TestCourier{
|
return &TestCourier{
|
||||||
ReqFor: map[string]*DeliverRequest{},
|
ReqFor: map[string]*deliverRequest{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,5 +127,5 @@ func (c dumbCourier) Deliver(from string, to string, data []byte) (error, bool)
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dumb courier, for when we just don't care about the result.
|
// DumbCourier always succeeds delivery, and ignores everything.
|
||||||
var DumbCourier = dumbCourier{}
|
var DumbCourier = dumbCourier{}
|
||||||
|
|||||||
Reference in New Issue
Block a user