-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement opcua-centrifugo command
- Loading branch information
1 parent
5f4cdc7
commit 2500e39
Showing
18 changed files
with
755 additions
and
255 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/cailloumajor/opcua-centrifugo/internal/centrifugo" | ||
"github.com/centrifugal/gocent/v3" | ||
) | ||
|
||
//go:generate moq -out intervals_mocks_test.go . CentrifugoChannels | ||
|
||
// CentrifugoChannels is a consumer contract modelling a Centrifugo channels query. | ||
type CentrifugoChannels interface { | ||
Channels(ctx context.Context, opts ...gocent.ChannelsOption) (gocent.ChannelsResult, error) | ||
} | ||
|
||
// ChannelIntervals returns the intervals of the Centrifugo channels in the given namespace. | ||
func ChannelIntervals(ctx context.Context, ch CentrifugoChannels, ns string) ([]time.Duration, error) { | ||
pat := fmt.Sprint(ns, centrifugo.NsSeparator, "*") | ||
res, err := ch.Channels(ctx, gocent.WithPattern(pat)) | ||
if err != nil { | ||
return nil, fmt.Errorf("error querying channels: %w", err) | ||
} | ||
|
||
var durations []time.Duration | ||
|
||
for chName := range res.Channels { | ||
c, err := centrifugo.ParseChannel(chName, ns) | ||
if err != nil { | ||
continue | ||
} | ||
durations = append(durations, c.Interval()) | ||
} | ||
|
||
return durations, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,69 @@ | ||
package main_test | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
. "github.com/cailloumajor/opcua-centrifugo/cmd/opcua-centrifugo" | ||
"github.com/cailloumajor/opcua-centrifugo/internal/testutils" | ||
"github.com/centrifugal/gocent/v3" | ||
) | ||
|
||
func TestChannelsInterval(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
channelsError bool | ||
expectError bool | ||
expectIntervals []time.Duration | ||
}{ | ||
{ | ||
name: "ChannelsError", | ||
channelsError: true, | ||
expectError: true, | ||
expectIntervals: nil, | ||
}, | ||
{ | ||
name: "Success", | ||
channelsError: false, | ||
expectError: false, | ||
expectIntervals: []time.Duration{2 * time.Second, 5 * time.Second}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
mockedCentrifugoChannels := &CentrifugoChannelsMock{ | ||
ChannelsFunc: func(ctx context.Context, opts ...gocent.ChannelsOption) (gocent.ChannelsResult, error) { | ||
options := &gocent.ChannelsOptions{} | ||
for _, opt := range opts { | ||
opt(options) | ||
} | ||
if got, want := options.Pattern, "ns:*"; got != want { | ||
t.Errorf("Channels call Pattern option: want %q, got %q", want, got) | ||
} | ||
if tc.channelsError { | ||
return gocent.ChannelsResult{}, testutils.ErrTesting | ||
} | ||
return gocent.ChannelsResult{ | ||
Channels: map[string]gocent.ChannelInfo{ | ||
"ns:ch1@2000": {}, | ||
"ch2": {}, | ||
"ns:ch3@5000": {}, | ||
}, | ||
}, nil | ||
}, | ||
} | ||
|
||
in, err := ChannelIntervals(context.Background(), mockedCentrifugoChannels, "ns") | ||
|
||
if msg := testutils.AssertError(t, err, tc.expectError); msg != "" { | ||
t.Error(msg) | ||
} | ||
if got, want := in, tc.expectIntervals; !reflect.DeepEqual(got, want) { | ||
t.Errorf("returned intervals, want %#q, got %#q", want, got) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.