aboutsummaryrefslogtreecommitdiffstats
path: root/lulutest/mock.go
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2026-08-05 19:25:03 -0230
committerSam Anthony <sam@samanthony.xyz>2026-08-05 19:25:03 -0230
commita2668014405c27671adeda4921431734afa8bec1 (patch)
tree87fae4038552c90372ae7a8a0887e3ac8cef4aad /lulutest/mock.go
parentabd51689cef03fa3daecb9abbb54b762e3ab02ac (diff)
downloadlulu-master.zip
lulutest: mock testsHEADv0.3.0mockmaster
Diffstat (limited to 'lulutest/mock.go')
-rw-r--r--lulutest/mock.go58
1 files changed, 39 insertions, 19 deletions
diff --git a/lulutest/mock.go b/lulutest/mock.go
index 15d3e36..95406d6 100644
--- a/lulutest/mock.go
+++ b/lulutest/mock.go
@@ -13,7 +13,7 @@ import (
"git.samanthony.xyz/lulu"
)
-type mockClient struct {
+type MockClient struct {
mu sync.Mutex
ints []lulu.InteriorValidation
covs []lulu.CoverValidation
@@ -21,13 +21,13 @@ type mockClient struct {
printables map[lulu.PrintableId]lulu.Printable
}
-func NewMockClient() lulu.Client {
- return &mockClient{
+func NewMockClient() *MockClient {
+ return &MockClient{
printables: make(map[lulu.PrintableId]lulu.Printable),
}
}
-func (c *mockClient) StartInteriorValidation(srcUrl string, mfg lulu.PkgId) (uint, error) {
+func (c *MockClient) StartInteriorValidation(srcUrl string, mfg lulu.PkgId) (uint, error) {
c.mu.Lock()
defer c.mu.Unlock()
id := uint(len(c.ints))
@@ -36,11 +36,11 @@ func (c *mockClient) StartInteriorValidation(srcUrl string, mfg lulu.PkgId) (uin
return id, nil
}
-func (c *mockClient) StartInteriorValidationBasic(srcUrl string) (uint, error) {
+func (c *MockClient) StartInteriorValidationBasic(srcUrl string) (uint, error) {
return c.StartInteriorValidation(srcUrl, lulu.PkgId{})
}
-func (c *mockClient) GetInteriorValidation(id uint) (lulu.InteriorValidation, error) {
+func (c *MockClient) GetInteriorValidation(id uint) (lulu.InteriorValidation, error) {
c.mu.Lock()
defer c.mu.Unlock()
if id >= uint(len(c.ints)) {
@@ -49,11 +49,11 @@ func (c *mockClient) GetInteriorValidation(id uint) (lulu.InteriorValidation, er
return c.ints[id], nil
}
-func (c *mockClient) CoverDimensions(mfg lulu.PkgId, npages uint, unit lulu.Unit) (lulu.CoverDimensions, error) {
+func (c *MockClient) CoverDimensions(mfg lulu.PkgId, npages uint, unit lulu.Unit) (lulu.CoverDimensions, error) {
return lulu.CoverDimensions{123.4, 56.78, unit}, nil
}
-func (c *mockClient) StartCoverValidation(srcUrl string, mfg lulu.PkgId, npages uint) (uint, error) {
+func (c *MockClient) StartCoverValidation(srcUrl string, mfg lulu.PkgId, npages uint) (uint, error) {
c.mu.Lock()
defer c.mu.Unlock()
id := uint(len(c.covs))
@@ -62,7 +62,7 @@ func (c *mockClient) StartCoverValidation(srcUrl string, mfg lulu.PkgId, npages
return id, nil
}
-func (c *mockClient) GetCoverValidation(id uint) (lulu.CoverValidation, error) {
+func (c *MockClient) GetCoverValidation(id uint) (lulu.CoverValidation, error) {
c.mu.Lock()
defer c.mu.Unlock()
if id >= uint(len(c.covs)) {
@@ -71,7 +71,7 @@ func (c *mockClient) GetCoverValidation(id uint) (lulu.CoverValidation, error) {
return c.covs[id], nil
}
-func (c *mockClient) Cost(items []lulu.CostLineItem, addr lulu.ShippingAddress, opt lulu.ShippingLevel) (lulu.Cost, lulu.ShippingAddressValidation, error) {
+func (c *MockClient) Cost(items []lulu.CostLineItem, addr lulu.ShippingAddress, opt lulu.ShippingLevel) (lulu.Cost, lulu.ShippingAddressValidation, error) {
var cost lulu.Cost
var subtotal decimal.Decimal
var tax decimal.Decimal
@@ -122,12 +122,21 @@ func (c *mockClient) Cost(items []lulu.CostLineItem, addr lulu.ShippingAddress,
type job struct {
j <-chan lulu.PrintJob
cancel chan chan bool
+ quit chan struct{}
}
+const (
+ shipDelay = time.Hour
+ deliverDelay = 56 * time.Hour
+)
+
func print(id uint64, eid string, contact lulu.EmailAddress, delay time.Duration, addr lulu.ShippingAddress, opt lulu.ShippingLevel, addrVal lulu.ShippingAddressValidation, items []lulu.LineItem, cost lulu.Cost) job {
jc := make(chan lulu.PrintJob)
cancel := make(chan chan bool)
+ quit := make(chan struct{})
go func() {
+ defer close(jc)
+ defer close(cancel)
j := lulu.PrintJob{
Contact: contact,
Cost: cost,
@@ -158,8 +167,8 @@ func print(id uint64, eid string, contact lulu.EmailAddress, delay time.Duration
}
setStatus(lulu.OrderProductionDelayed)
prod := time.After(delay)
- ship := time.After(delay + time.Hour)
- arrive := time.After(delay + time.Hour + 56*time.Hour)
+ ship := time.After(delay + shipDelay)
+ arrive := time.After(delay + shipDelay + deliverDelay)
for {
select {
case jc <- j:
@@ -182,13 +191,24 @@ func print(id uint64, eid string, contact lulu.EmailAddress, delay time.Duration
if j.Status.Status == lulu.OrderShipped {
setStatus(lulu.OrderDelivered)
}
+ case <-quit:
+ return
}
}
}()
- return job{jc, cancel}
+ return job{jc, cancel, quit}
+}
+
+// Close terminates the client's goroutines.
+func (c *MockClient) Close() {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ for _, j := range c.jobs {
+ close(j.quit)
+ }
}
-func (c *mockClient) Print(contact lulu.EmailAddress, eid string, delay time.Duration, addr lulu.ShippingAddress, opt lulu.ShippingLevel, items []lulu.Printable) (lulu.PrintJob, error) {
+func (c *MockClient) Print(contact lulu.EmailAddress, eid string, delay time.Duration, addr lulu.ShippingAddress, opt lulu.ShippingLevel, items []lulu.Printable) (lulu.PrintJob, error) {
c.mu.Lock()
defer c.mu.Unlock()
@@ -213,7 +233,7 @@ func (c *mockClient) Print(contact lulu.EmailAddress, eid string, delay time.Dur
return c.print(id, eid, contact, delay, addr, opt, lineItems, costItems)
}
-func (c *mockClient) Reprint(contact lulu.EmailAddress, eid string, delay time.Duration, addr lulu.ShippingAddress, opt lulu.ShippingLevel, items []lulu.Reprintable) (lulu.PrintJob, error) {
+func (c *MockClient) Reprint(contact lulu.EmailAddress, eid string, delay time.Duration, addr lulu.ShippingAddress, opt lulu.ShippingLevel, items []lulu.Reprintable) (lulu.PrintJob, error) {
c.mu.Lock()
defer c.mu.Unlock()
@@ -241,7 +261,7 @@ func (c *mockClient) Reprint(contact lulu.EmailAddress, eid string, delay time.D
return c.print(id, eid, contact, delay, addr, opt, lineItems, costItems)
}
-func (c *mockClient) print(id uint64, eid string, contact lulu.EmailAddress, delay time.Duration, addr lulu.ShippingAddress, opt lulu.ShippingLevel, lineItems []lulu.LineItem, costItems []lulu.CostLineItem) (lulu.PrintJob, error) {
+func (c *MockClient) print(id uint64, eid string, contact lulu.EmailAddress, delay time.Duration, addr lulu.ShippingAddress, opt lulu.ShippingLevel, lineItems []lulu.LineItem, costItems []lulu.CostLineItem) (lulu.PrintJob, error) {
cost, addrVal, err := c.Cost(costItems, addr, opt)
if err != nil {
return lulu.PrintJob{}, err
@@ -251,7 +271,7 @@ func (c *mockClient) print(id uint64, eid string, contact lulu.EmailAddress, del
return <-j.j, nil
}
-func (c *mockClient) Job(id uint64) (lulu.PrintJob, error) {
+func (c *MockClient) Job(id uint64) (lulu.PrintJob, error) {
c.mu.Lock()
defer c.mu.Unlock()
if id < uint64(len(c.jobs)) {
@@ -261,7 +281,7 @@ func (c *mockClient) Job(id uint64) (lulu.PrintJob, error) {
}
// Ignores queries
-func (c *mockClient) Jobs(qs ...lulu.PrintJobQuery) ([]lulu.PrintJob, error) {
+func (c *MockClient) Jobs(qs ...lulu.PrintJobQuery) ([]lulu.PrintJob, error) {
c.mu.Lock()
defer c.mu.Unlock()
js := make([]lulu.PrintJob, len(c.jobs))
@@ -271,7 +291,7 @@ func (c *mockClient) Jobs(qs ...lulu.PrintJobQuery) ([]lulu.PrintJob, error) {
return js, nil
}
-func (c *mockClient) Cancel(id uint64) error {
+func (c *MockClient) Cancel(id uint64) error {
c.mu.Lock()
defer c.mu.Unlock()
if id < uint64(len(c.jobs)) {