Skip to content

Commit

Permalink
Updates to include end-to-end tests for MaxMsgLength
Browse files Browse the repository at this point in the history
  • Loading branch information
azarc-jono-langley committed Nov 3, 2022
1 parent f6f2219 commit 44b5720
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 17 deletions.
3 changes: 2 additions & 1 deletion dummy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// +build ignore+
//go:build ignore
// +build ignore

package main

Expand Down
8 changes: 5 additions & 3 deletions jms20subset/ConnectionFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ type ConnectionFactory interface {

// CreateContext creates a connection to the messaging provider using the
// configuration parameters that are encapsulated by this ConnectionFactory.
// Optional options can be provided to configure the connection prior to initialisation.
//
// Defaults to sessionMode of JMSContextAUTOACKNOWLEDGE
CreateContext() (JMSContext, JMSException)
CreateContext(opts ...MQOptions) (JMSContext, JMSException)

// CreateContextWithSessionMode creates a connection to the messaging provider using the
// configuration parameters that are encapsulated by this ConnectionFactory,
// and the specified session mode.
CreateContextWithSessionMode(sessionMode int) (JMSContext, JMSException)
// and the specified session mode. Optional options can be provided to configure the
// connection prior to initialisation.
CreateContextWithSessionMode(sessionMode int, opts ...MQOptions) (JMSContext, JMSException)
}
11 changes: 11 additions & 0 deletions jms20subset/MQOptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package jms20subset

import "github.com/ibm-messaging/mq-golang/v5/ibmmq"

type MQOptions func(cno *ibmmq.MQCNO)

func WithMaxMsgLength(maxMsgLength int32) MQOptions {
return func(cno *ibmmq.MQCNO) {
cno.ClientConn.MaxMsgLength = maxMsgLength
}
}
49 changes: 46 additions & 3 deletions mq_connection_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package main

import (
"github.com/ibm-messaging/mq-golang-jms20/jms20subset"
"github.com/ibm-messaging/mq-golang/v5/ibmmq"
"testing"

Expand All @@ -18,17 +19,18 @@ import (
)

func TestMQConnectionOptions(t *testing.T) {
t.Run("MaxMsgLength", func(t *testing.T) {

t.Run("MaxMsgLength set on CreateContext", func(t *testing.T) {
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, cfErr)

// Ensure that the options were applied when setting connection options on Context creation
msg := "options were not applied"
context, ctxErr := cf.CreateContext(
mqjms.WithMaxMsgLength(2000),
jms20subset.WithMaxMsgLength(2000),
func(cno *ibmmq.MQCNO) {
assert.Equal(t, 2000, cno.ClientConn.MaxMsgLength)
assert.Equal(t, int32(2000), cno.ClientConn.MaxMsgLength)
msg = "options applied"
},
)
Expand All @@ -40,4 +42,45 @@ func TestMQConnectionOptions(t *testing.T) {

assert.Equal(t, "options applied", msg)
})

t.Run("MaxMsgLength is respected when receive message on CreateContextWithSessionMode", func(t *testing.T) {
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, cfErr)

sContext, ctxErr := cf.CreateContext()
assert.Nil(t, ctxErr)
if sContext != nil {
defer sContext.Close()
}

// Create a Queue object that points at an IBM MQ queue
sQueue := sContext.CreateQueue("DEV.QUEUE.1")
// Send a message to the queue that contains a large string
errSend := sContext.CreateProducer().SendString(sQueue, "This has more than data than the max message length")
assert.NoError(t, errSend)

// create consumer with low msg length
rContext, ctxErr := cf.CreateContextWithSessionMode(
jms20subset.JMSContextAUTOACKNOWLEDGE,
jms20subset.WithMaxMsgLength(20),
)
assert.Nil(t, ctxErr)
if rContext != nil {
defer rContext.Close()
}

// Create a Queue object that points at an IBM MQ queue
rQueue := rContext.CreateQueue("DEV.QUEUE.1")
// Send a message to the queue that contains a large string
consumer, errSend := rContext.CreateConsumer(rQueue)
assert.NoError(t, errSend)

// expect that receiving the message will cause an JMS Data Length error
_, err := consumer.ReceiveStringBodyNoWait()
assert.Error(t, err)
jmsErr, ok := err.(jms20subset.JMSExceptionImpl)
assert.True(t, ok)
assert.Equal(t, "MQRC_DATA_LENGTH_ERROR", jmsErr.GetReason())
})
}
12 changes: 2 additions & 10 deletions mqjms/ConnectionFactoryImpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ type ConnectionFactoryImpl struct {

// CreateContext implements the JMS method to create a connection to an IBM MQ
// queue manager.
func (cf ConnectionFactoryImpl) CreateContext(mqos ...MQOptions) (jms20subset.JMSContext, jms20subset.JMSException) {
func (cf ConnectionFactoryImpl) CreateContext(mqos ...jms20subset.MQOptions) (jms20subset.JMSContext, jms20subset.JMSException) {
return cf.CreateContextWithSessionMode(jms20subset.JMSContextAUTOACKNOWLEDGE, mqos...)
}

// CreateContextWithSessionMode implements the JMS method to create a connection to an IBM MQ
// queue manager using the specified session mode.
func (cf ConnectionFactoryImpl) CreateContextWithSessionMode(sessionMode int, mqos ...MQOptions) (jms20subset.JMSContext, jms20subset.JMSException) {
func (cf ConnectionFactoryImpl) CreateContextWithSessionMode(sessionMode int, mqos ...jms20subset.MQOptions) (jms20subset.JMSContext, jms20subset.JMSException) {

// Allocate the internal structures required to create an connection to IBM MQ.
cno := ibmmq.NewMQCNO()
Expand Down Expand Up @@ -175,11 +175,3 @@ func (cf ConnectionFactoryImpl) CreateContextWithSessionMode(sessionMode int, mq
return ctx, retErr

}

type MQOptions func(cno *ibmmq.MQCNO)

func WithMaxMsgLength(maxMsgLength int32) MQOptions {
return func(cno *ibmmq.MQCNO) {
cno.ClientConn.MaxMsgLength = maxMsgLength
}
}

0 comments on commit 44b5720

Please sign in to comment.