| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | type countingSender struct{ calls int } |
| 11 | |
| 12 | func (c *countingSender) Send(context.Context, string, string, string) error { |
| 13 | c.calls++ |
| 14 | return nil |
| 15 | } |
| 16 | |
| 17 | type refusingSender struct{} |
| 18 | |
| 19 | func (refusingSender) Send(context.Context, string, string, string) error { |
| 20 | return errors.New("send to a@example.com: 550 no such user") |
| 21 | } |
| 22 | |
| 23 | // A budget already spent before the sender is called is a DEFERRAL, |
| 24 | // never a counted attempt: no relay was asked (code review 4, B.1). |
| 25 | func TestASpentBudgetDefersRatherThanFails(t *testing.T) { |
| 26 | next := &countingSender{} |
| 27 | b := newBoundedSender(next) |
| 28 | ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Second)) |
| 29 | defer cancel() |
| 30 | err := b.Send(ctx, "someone@example.com", "s", "b") |
| 31 | if !errors.Is(err, ErrMailBusy) { |
| 32 | t.Errorf("spent budget: %v, want ErrMailBusy", err) |
| 33 | } |
| 34 | if next.calls != 0 { |
| 35 | t.Error("the sender was called with no budget") |
| 36 | } |
| 37 | if len(b.slots) != 0 { |
| 38 | t.Error("the slot was not released") |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // The sender's own error text stops at the wrapper (code review 2, B1). |
| 43 | func TestTheSendersErrorTextStopsAtTheWrapper(t *testing.T) { |
| 44 | err := newBoundedSender(refusingSender{}).Send(context.Background(), "a@example.com", "s", "b") |
| 45 | if !errors.Is(err, ErrMailRefused) || err.Error() != ErrMailRefused.Error() { |
| 46 | t.Errorf("a refused send: %v, want the content-free ErrMailRefused", err) |
| 47 | } |
| 48 | } |
| 49 | |