Skip to content

Commit 44b5720

Browse files
Updates to include end-to-end tests for MaxMsgLength
1 parent f6f2219 commit 44b5720

File tree

5 files changed

+66
-17
lines changed

5 files changed

+66
-17
lines changed

dummy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// +build ignore+
1+
//go:build ignore
2+
// +build ignore
23

34
package main
45

jms20subset/ConnectionFactory.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ type ConnectionFactory interface {
1818

1919
// CreateContext creates a connection to the messaging provider using the
2020
// configuration parameters that are encapsulated by this ConnectionFactory.
21+
// Optional options can be provided to configure the connection prior to initialisation.
2122
//
2223
// Defaults to sessionMode of JMSContextAUTOACKNOWLEDGE
23-
CreateContext() (JMSContext, JMSException)
24+
CreateContext(opts ...MQOptions) (JMSContext, JMSException)
2425

2526
// CreateContextWithSessionMode creates a connection to the messaging provider using the
2627
// configuration parameters that are encapsulated by this ConnectionFactory,
27-
// and the specified session mode.
28-
CreateContextWithSessionMode(sessionMode int) (JMSContext, JMSException)
28+
// and the specified session mode. Optional options can be provided to configure the
29+
// connection prior to initialisation.
30+
CreateContextWithSessionMode(sessionMode int, opts ...MQOptions) (JMSContext, JMSException)
2931
}

jms20subset/MQOptions.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package jms20subset
2+
3+
import "github.com/ibm-messaging/mq-golang/v5/ibmmq"
4+
5+
type MQOptions func(cno *ibmmq.MQCNO)
6+
7+
func WithMaxMsgLength(maxMsgLength int32) MQOptions {
8+
return func(cno *ibmmq.MQCNO) {
9+
cno.ClientConn.MaxMsgLength = maxMsgLength
10+
}
11+
}

mq_connection_options_test.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package main
1111

1212
import (
13+
"github.com/ibm-messaging/mq-golang-jms20/jms20subset"
1314
"github.com/ibm-messaging/mq-golang/v5/ibmmq"
1415
"testing"
1516

@@ -18,17 +19,18 @@ import (
1819
)
1920

2021
func TestMQConnectionOptions(t *testing.T) {
21-
t.Run("MaxMsgLength", func(t *testing.T) {
22+
23+
t.Run("MaxMsgLength set on CreateContext", func(t *testing.T) {
2224
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
2325
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
2426
assert.Nil(t, cfErr)
2527

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

4143
assert.Equal(t, "options applied", msg)
4244
})
45+
46+
t.Run("MaxMsgLength is respected when receive message on CreateContextWithSessionMode", func(t *testing.T) {
47+
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
48+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
49+
assert.Nil(t, cfErr)
50+
51+
sContext, ctxErr := cf.CreateContext()
52+
assert.Nil(t, ctxErr)
53+
if sContext != nil {
54+
defer sContext.Close()
55+
}
56+
57+
// Create a Queue object that points at an IBM MQ queue
58+
sQueue := sContext.CreateQueue("DEV.QUEUE.1")
59+
// Send a message to the queue that contains a large string
60+
errSend := sContext.CreateProducer().SendString(sQueue, "This has more than data than the max message length")
61+
assert.NoError(t, errSend)
62+
63+
// create consumer with low msg length
64+
rContext, ctxErr := cf.CreateContextWithSessionMode(
65+
jms20subset.JMSContextAUTOACKNOWLEDGE,
66+
jms20subset.WithMaxMsgLength(20),
67+
)
68+
assert.Nil(t, ctxErr)
69+
if rContext != nil {
70+
defer rContext.Close()
71+
}
72+
73+
// Create a Queue object that points at an IBM MQ queue
74+
rQueue := rContext.CreateQueue("DEV.QUEUE.1")
75+
// Send a message to the queue that contains a large string
76+
consumer, errSend := rContext.CreateConsumer(rQueue)
77+
assert.NoError(t, errSend)
78+
79+
// expect that receiving the message will cause an JMS Data Length error
80+
_, err := consumer.ReceiveStringBodyNoWait()
81+
assert.Error(t, err)
82+
jmsErr, ok := err.(jms20subset.JMSExceptionImpl)
83+
assert.True(t, ok)
84+
assert.Equal(t, "MQRC_DATA_LENGTH_ERROR", jmsErr.GetReason())
85+
})
4386
}

mqjms/ConnectionFactoryImpl.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ type ConnectionFactoryImpl struct {
5959

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

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

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

177177
}
178-
179-
type MQOptions func(cno *ibmmq.MQCNO)
180-
181-
func WithMaxMsgLength(maxMsgLength int32) MQOptions {
182-
return func(cno *ibmmq.MQCNO) {
183-
cno.ClientConn.MaxMsgLength = maxMsgLength
184-
}
185-
}

0 commit comments

Comments
 (0)