Skip to content

Commit

Permalink
Merge pull request #64 from muzzammilshahid/interop-pubsub-messages
Browse files Browse the repository at this point in the history
Add interoperability tests for pubsub messages
  • Loading branch information
muzzammilshahid authored Jul 31, 2024
2 parents 30b0691 + 9ef7409 commit 55b121b
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/interop/messages/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package messages_test

import (
"fmt"
"reflect"
"testing"

"github.com/stretchr/testify/require"

"github.com/xconnio/wampproto-go/messages"
"github.com/xconnio/wampproto-go/serializers"
"github.com/xconnio/wampproto-go/tests"
)

func eventsEqual(msg1 *messages.Event, msg2 *messages.Event) bool {
return msg1.SubscriptionID() == msg2.SubscriptionID() &&
msg1.PublicationID() == msg2.PublicationID() &&
reflect.DeepEqual(msg1.Details(), msg2.Details()) &&
reflect.DeepEqual(msg1.Args(), msg2.Args()) &&
reflect.DeepEqual(msg1.KwArgs(), msg2.KwArgs())
}

func testEventMessage(t *testing.T, serializerStr string, serializer serializers.Serializer) {
var message = messages.NewEvent(1, 1, map[string]any{"foo": true}, []any{"abc"}, map[string]any{"abc": "xyz"})
command := fmt.Sprintf("message event 1 1 abc -d foo=true -k abc=xyz --serializer %s --output hex", serializerStr)

msg := tests.RunCommandAndDeserialize(t, command, serializer)
require.True(t, eventsEqual(message, msg.(*messages.Event)))
}

func TestEventMessage(t *testing.T) {
t.Run("JSONSerializer", func(t *testing.T) {
serializer := &serializers.JSONSerializer{}
testEventMessage(t, "json", serializer)
})

t.Run("CBORSerializer", func(t *testing.T) {
serializer := &serializers.CBORSerializer{}
testEventMessage(t, "cbor", serializer)
})

t.Run("MsgSerializer", func(t *testing.T) {
serializer := &serializers.MsgPackSerializer{}
testEventMessage(t, "msgpack", serializer)
})
}
46 changes: 46 additions & 0 deletions tests/interop/messages/publish_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package messages_test

import (
"fmt"
"reflect"
"testing"

"github.com/stretchr/testify/require"

"github.com/xconnio/wampproto-go/messages"
"github.com/xconnio/wampproto-go/serializers"
"github.com/xconnio/wampproto-go/tests"
)

func publishEqual(msg1 *messages.Publish, msg2 *messages.Publish) bool {
return msg1.RequestID() == msg2.RequestID() &&
msg1.Topic() == msg2.Topic() &&
reflect.DeepEqual(msg1.Options(), msg2.Options()) &&
reflect.DeepEqual(msg1.Args(), msg2.Args()) &&
reflect.DeepEqual(msg1.KwArgs(), msg2.KwArgs())
}

func testPublishMessage(t *testing.T, serializerStr string, serializer serializers.Serializer) {
var message = messages.NewPublish(1, map[string]any{"abc": "xyz"}, "test", []any{"abc"}, map[string]any{"abc": "xyz"})
command := fmt.Sprintf("message publish 1 test abc -o abc:xyz -k abc=xyz --serializer %s --output hex", serializerStr)

msg := tests.RunCommandAndDeserialize(t, command, serializer)
require.True(t, publishEqual(message, msg.(*messages.Publish)))
}

func TestPublishMessage(t *testing.T) {
t.Run("JSONSerializer", func(t *testing.T) {
serializer := &serializers.JSONSerializer{}
testPublishMessage(t, "json", serializer)
})

t.Run("CBORSerializer", func(t *testing.T) {
serializer := &serializers.CBORSerializer{}
testPublishMessage(t, "cbor", serializer)
})

t.Run("MsgPackSerializer", func(t *testing.T) {
serializer := &serializers.MsgPackSerializer{}
testPublishMessage(t, "msgpack", serializer)
})
}
42 changes: 42 additions & 0 deletions tests/interop/messages/published_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package messages_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/xconnio/wampproto-go/messages"
"github.com/xconnio/wampproto-go/serializers"
"github.com/xconnio/wampproto-go/tests"
)

func publishedEqual(msg1 *messages.Published, msg2 *messages.Published) bool {
return msg1.RequestID() == msg2.RequestID() &&
msg1.PublicationID() == msg2.PublicationID()
}

func testPublishedMessage(t *testing.T, serializerStr string, serializer serializers.Serializer) {
var message = messages.NewPublished(1, 1)
command := fmt.Sprintf("message published 1 1 --serializer %s --output hex", serializerStr)

msg := tests.RunCommandAndDeserialize(t, command, serializer)
require.True(t, publishedEqual(message, msg.(*messages.Published)))
}

func TestPublishedMessage(t *testing.T) {
t.Run("JSONSerializer", func(t *testing.T) {
serializer := &serializers.JSONSerializer{}
testPublishedMessage(t, "json", serializer)
})

t.Run("CBORSerializer", func(t *testing.T) {
serializer := &serializers.CBORSerializer{}
testPublishedMessage(t, "cbor", serializer)
})

t.Run("MsgPackSerializer", func(t *testing.T) {
serializer := &serializers.MsgPackSerializer{}
testPublishedMessage(t, "msgpack", serializer)
})
}
44 changes: 44 additions & 0 deletions tests/interop/messages/subscribe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package messages_test

import (
"fmt"
"reflect"
"testing"

"github.com/stretchr/testify/require"

"github.com/xconnio/wampproto-go/messages"
"github.com/xconnio/wampproto-go/serializers"
"github.com/xconnio/wampproto-go/tests"
)

func subscribesEqual(msg1 *messages.Subscribe, msg2 *messages.Subscribe) bool {
return msg1.RequestID() == msg2.RequestID() &&
msg1.Topic() == msg2.Topic() &&
reflect.DeepEqual(msg1.Options(), msg2.Options())
}

func testSubscribeMessage(t *testing.T, serializerStr string, serializer serializers.Serializer) {
var message = messages.NewSubscribe(1, map[string]any{"abc": "xyz"}, "test")
command := fmt.Sprintf("message subscribe 1 test -o abc=xyz --serializer %s --output hex", serializerStr)

msg := tests.RunCommandAndDeserialize(t, command, serializer)
require.True(t, subscribesEqual(message, msg.(*messages.Subscribe)))
}

func TestSubscribeMessage(t *testing.T) {
t.Run("JSONSerializer", func(t *testing.T) {
serializer := &serializers.JSONSerializer{}
testSubscribeMessage(t, "json", serializer)
})

t.Run("CBORSerializer", func(t *testing.T) {
serializer := &serializers.CBORSerializer{}
testSubscribeMessage(t, "cbor", serializer)
})

t.Run("MsgPackSerializer", func(t *testing.T) {
serializer := &serializers.MsgPackSerializer{}
testSubscribeMessage(t, "msgpack", serializer)
})
}
42 changes: 42 additions & 0 deletions tests/interop/messages/subscribed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package messages_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/xconnio/wampproto-go/messages"
"github.com/xconnio/wampproto-go/serializers"
"github.com/xconnio/wampproto-go/tests"
)

func subscribedEqual(msg1 *messages.Subscribed, msg2 *messages.Subscribed) bool {
return msg1.RequestID() == msg2.RequestID() &&
msg1.SubscriptionID() == msg2.SubscriptionID()
}

func testSubscribedMessage(t *testing.T, serializerStr string, serializer serializers.Serializer) {
var message = messages.NewSubscribed(1, 1)
command := fmt.Sprintf("message subscribed 1 1 --serializer %s --output hex", serializerStr)

msg := tests.RunCommandAndDeserialize(t, command, serializer)
require.True(t, subscribedEqual(message, msg.(*messages.Subscribed)))
}

func TestSubscribedMessage(t *testing.T) {
t.Run("JSONSerializer", func(t *testing.T) {
serializer := &serializers.JSONSerializer{}
testSubscribedMessage(t, "json", serializer)
})

t.Run("CBORSerializer", func(t *testing.T) {
serializer := &serializers.CBORSerializer{}
testSubscribedMessage(t, "cbor", serializer)
})

t.Run("MsgPackSerializer", func(t *testing.T) {
serializer := &serializers.MsgPackSerializer{}
testSubscribedMessage(t, "msgpack", serializer)
})
}
42 changes: 42 additions & 0 deletions tests/interop/messages/unsubscribe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package messages_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/xconnio/wampproto-go/messages"
"github.com/xconnio/wampproto-go/serializers"
"github.com/xconnio/wampproto-go/tests"
)

func unsubscribeEqual(msg1 *messages.Unsubscribe, msg2 *messages.Unsubscribe) bool {
return msg1.RequestID() == msg2.RequestID() &&
msg1.SubscriptionID() == msg2.SubscriptionID()
}

func testUnsubscribeMessage(t *testing.T, serializerStr string, serializer serializers.Serializer) {
var message = messages.NewUnsubscribe(1, 1)
command := fmt.Sprintf("message unsubscribe 1 1 --serializer %s --output hex", serializerStr)

msg := tests.RunCommandAndDeserialize(t, command, serializer)
require.True(t, unsubscribeEqual(message, msg.(*messages.Unsubscribe)))
}

func TestUnsubscribeMessage(t *testing.T) {
t.Run("JSONSerializer", func(t *testing.T) {
serializer := &serializers.JSONSerializer{}
testUnsubscribeMessage(t, "json", serializer)
})

t.Run("CBORSerializer", func(t *testing.T) {
serializer := &serializers.CBORSerializer{}
testUnsubscribeMessage(t, "cbor", serializer)
})

t.Run("MsgPackSerializer", func(t *testing.T) {
serializer := &serializers.MsgPackSerializer{}
testUnsubscribeMessage(t, "msgpack", serializer)
})
}
41 changes: 41 additions & 0 deletions tests/interop/messages/unsubscribed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package messages_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/xconnio/wampproto-go/messages"
"github.com/xconnio/wampproto-go/serializers"
"github.com/xconnio/wampproto-go/tests"
)

func unsubscribedEqual(msg1 *messages.Unsubscribed, msg2 *messages.Unsubscribed) bool {
return msg1.RequestID() == msg2.RequestID()
}

func testUnsubscribedMessage(t *testing.T, serializerStr string, serializer serializers.Serializer) {
var message = messages.NewUnsubscribed(1)
command := fmt.Sprintf("message unsubscribed 1 --serializer %s --output hex", serializerStr)

msg := tests.RunCommandAndDeserialize(t, command, serializer)
require.True(t, unsubscribedEqual(message, msg.(*messages.Unsubscribed)))
}

func TestUnsubscribedMessage(t *testing.T) {
t.Run("JSONSerializer", func(t *testing.T) {
serializer := &serializers.JSONSerializer{}
testUnsubscribedMessage(t, "json", serializer)
})

t.Run("CBORSerializer", func(t *testing.T) {
serializer := &serializers.CBORSerializer{}
testUnsubscribedMessage(t, "cbor", serializer)
})

t.Run("MsgPackSerializer", func(t *testing.T) {
serializer := &serializers.MsgPackSerializer{}
testUnsubscribedMessage(t, "msgpack", serializer)
})
}

0 comments on commit 55b121b

Please sign in to comment.