From 44b5720c2c694ef686cd737512378a42d11f85a7 Mon Sep 17 00:00:00 2001 From: Jonathan Langley Date: Wed, 2 Nov 2022 12:25:12 +0100 Subject: [PATCH] Updates to include end-to-end tests for MaxMsgLength --- dummy.go | 3 +- jms20subset/ConnectionFactory.go | 8 ++++-- jms20subset/MQOptions.go | 11 +++++++ mq_connection_options_test.go | 49 ++++++++++++++++++++++++++++++-- mqjms/ConnectionFactoryImpl.go | 12 ++------ 5 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 jms20subset/MQOptions.go diff --git a/dummy.go b/dummy.go index b919978..c084f17 100644 --- a/dummy.go +++ b/dummy.go @@ -1,4 +1,5 @@ -// +build ignore+ +//go:build ignore +// +build ignore package main diff --git a/jms20subset/ConnectionFactory.go b/jms20subset/ConnectionFactory.go index affc52f..c564bee 100644 --- a/jms20subset/ConnectionFactory.go +++ b/jms20subset/ConnectionFactory.go @@ -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) } diff --git a/jms20subset/MQOptions.go b/jms20subset/MQOptions.go new file mode 100644 index 0000000..51094ba --- /dev/null +++ b/jms20subset/MQOptions.go @@ -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 + } +} diff --git a/mq_connection_options_test.go b/mq_connection_options_test.go index 3b31209..c861f49 100644 --- a/mq_connection_options_test.go +++ b/mq_connection_options_test.go @@ -10,6 +10,7 @@ package main import ( + "github.com/ibm-messaging/mq-golang-jms20/jms20subset" "github.com/ibm-messaging/mq-golang/v5/ibmmq" "testing" @@ -18,7 +19,8 @@ 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) @@ -26,9 +28,9 @@ func TestMQConnectionOptions(t *testing.T) { // 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" }, ) @@ -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()) + }) } diff --git a/mqjms/ConnectionFactoryImpl.go b/mqjms/ConnectionFactoryImpl.go index 52d7280..3c3a6dc 100644 --- a/mqjms/ConnectionFactoryImpl.go +++ b/mqjms/ConnectionFactoryImpl.go @@ -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() @@ -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 - } -}