Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MM-51037] Add plugin cluster event method #127

Merged
merged 2 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ type Client struct {
api plugin.API

Bot BotService
Configuration ConfigurationService
Channel ChannelService
Cluster ClusterService
Configuration ConfigurationService
SlashCommand SlashCommandService
OAuth OAuthService
Emoji EmojiService
Expand Down Expand Up @@ -41,6 +42,7 @@ func NewClient(api plugin.API, driver plugin.Driver) *Client {

Bot: BotService{api: api},
Channel: ChannelService{api: api},
Cluster: ClusterService{api: api},
Configuration: ConfigurationService{api: api},
SlashCommand: SlashCommandService{api: api},
OAuth: OAuthService{api: api},
Expand Down
23 changes: 23 additions & 0 deletions cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package pluginapi

import (
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/plugin"
)

// ClusterService exposes methods to interact with cluster nodes.
type ClusterService struct {
api plugin.API
}

// ClusterService broadcasts a plugin event to all other running instances of
// the calling plugin that are present in the cluster.
//
// This method is used to allow plugin communication in a High-Availability cluster.
// The receiving side should implement the OnPluginClusterEvent hook
// to receive events sent through this method.
//
// Minimum server version: 5.36
func (c *ClusterService) PublishPluginEvent(ev model.PluginClusterEvent, opts model.PluginClusterEventSendOptions) error {
return c.api.PublishPluginClusterEvent(ev, opts)
}
49 changes: 49 additions & 0 deletions cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package pluginapi_test

import (
"errors"
"testing"

"github.com/stretchr/testify/require"

"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/plugin/plugintest"

pluginapi "github.com/mattermost/mattermost-plugin-api"
)

func TestPublishPluginClusterEvent(t *testing.T) {
t.Run("success", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
client := pluginapi.NewClient(api, &plugintest.Driver{})

api.On("PublishPluginClusterEvent",
model.PluginClusterEvent{Id: "someID", Data: []byte("foo")},
model.PluginClusterEventSendOptions{SendType: model.PluginClusterEventSendTypeReliable},
).Return(nil)

err := client.Cluster.PublishPluginEvent(
model.PluginClusterEvent{Id: "someID", Data: []byte("foo")},
model.PluginClusterEventSendOptions{SendType: model.PluginClusterEventSendTypeReliable},
)
require.NoError(t, err)
})

t.Run("failure", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
client := pluginapi.NewClient(api, &plugintest.Driver{})

api.On("PublishPluginClusterEvent",
model.PluginClusterEvent{Id: "someID", Data: []byte("foo")},
model.PluginClusterEventSendOptions{SendType: model.PluginClusterEventSendTypeReliable},
).Return(errors.New("someError"))

err := client.Cluster.PublishPluginEvent(
model.PluginClusterEvent{Id: "someID", Data: []byte("foo")},
model.PluginClusterEventSendOptions{SendType: model.PluginClusterEventSendTypeReliable},
)
require.Error(t, err)
})
}