-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
9 changed files
with
170 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package notifier | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/favonia/cloudflare-ddns/internal/pp" | ||
) | ||
|
||
// Composed represents the composite of multiple notifiers. | ||
type Composed []Notifier | ||
|
||
var _ Notifier = Composed{} | ||
|
||
// NewComposed creates a new composed notifier. | ||
func NewComposed(nots ...Notifier) Composed { | ||
ns := make([]Notifier, 0, len(nots)) | ||
for _, n := range nots { | ||
if n == nil { | ||
continue | ||
} | ||
if list, composed := n.(Composed); composed { | ||
ns = append(ns, list...) | ||
} else { | ||
ns = append(ns, n) | ||
} | ||
} | ||
return Composed(ns) | ||
} | ||
|
||
// Describe calls [Notifier.Describe] for each notifier in the group with the callback. | ||
func (ns Composed) Describe(yield func(name string, params string) bool) { | ||
for _, n := range ns { | ||
for name, params := range n.Describe { | ||
if !yield(name, params) { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Send calls [Notifier.Send] for each notifier in the group. | ||
func (ns Composed) Send(ctx context.Context, ppfmt pp.PP, msg Message) bool { | ||
ok := true | ||
for _, n := range ns { | ||
ok = ok && n.Send(ctx, ppfmt, msg) | ||
} | ||
return ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package notifier_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.uber.org/mock/gomock" | ||
|
||
"github.com/favonia/cloudflare-ddns/internal/mocks" | ||
"github.com/favonia/cloudflare-ddns/internal/notifier" | ||
) | ||
|
||
func TestComposedDescribe(t *testing.T) { | ||
t.Parallel() | ||
|
||
mockCtrl := gomock.NewController(t) | ||
|
||
ns1 := make([]notifier.Notifier, 0, 3) | ||
for range 3 { | ||
n := mocks.NewMockNotifier(mockCtrl) | ||
n.EXPECT().Describe(gomock.Any()).DoAndReturn( | ||
func(yield func(string, string) bool) { | ||
yield("name", "params") | ||
}, | ||
) | ||
ns1 = append(ns1, n) | ||
} | ||
ns2 := make([]notifier.Notifier, 0, 2) | ||
for range 2 { | ||
n := mocks.NewMockNotifier(mockCtrl) | ||
ns2 = append(ns2, n) | ||
} | ||
c := notifier.NewComposed(notifier.NewComposed(ns1...), notifier.NewComposed(ns2...)) | ||
|
||
count := 0 | ||
for range c.Describe { | ||
count++ | ||
if count >= 3 { | ||
break | ||
} | ||
} | ||
require.Equal(t, 3, count) | ||
} | ||
|
||
func TestComposedSend(t *testing.T) { | ||
t.Parallel() | ||
|
||
for name, tc := range map[string]struct { | ||
ss []string | ||
}{ | ||
"nil": {nil}, | ||
"empty": {[]string{}}, | ||
"one": {[]string{"hi"}}, | ||
"two": {[]string{"hi", "hey"}}, | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ns := make([]notifier.Notifier, 0, 5) | ||
mockCtrl := gomock.NewController(t) | ||
mockPP := mocks.NewMockPP(mockCtrl) | ||
|
||
msg := notifier.Message(tc.ss) | ||
|
||
for range 5 { | ||
n := mocks.NewMockNotifier(mockCtrl) | ||
n.EXPECT().Send(context.Background(), mockPP, msg).Return(true) | ||
ns = append(ns, n) | ||
} | ||
|
||
ok := notifier.NewComposed(ns...).Send(context.Background(), mockPP, msg) | ||
require.True(t, ok) | ||
}) | ||
} | ||
} |