|
| 1 | +/* |
| 2 | + * Copyright (c) IBM Corporation 2019 |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + */ |
| 10 | +package main |
| 11 | + |
| 12 | +import ( |
| 13 | + "fmt" |
| 14 | + "github.com/ibm-messaging/mq-golang-jms20/mqjms" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "os" |
| 17 | + "testing" |
| 18 | +) |
| 19 | + |
| 20 | +/* |
| 21 | + * Test the use of a local bindings connection to the queue manager, as |
| 22 | + * opposed to a remote client connection. |
| 23 | + * |
| 24 | + * Assumes a locally running queue manager called QM1, with a defined local |
| 25 | + * queue called LOCAL.QUEUE |
| 26 | + */ |
| 27 | +func TestLocalBindingsConnect(t *testing.T) { |
| 28 | + |
| 29 | + // Checking the presence of the mqs.ini file is a reasonably reliable way of |
| 30 | + // identifying if there is a local queue manager installation, so that we |
| 31 | + // can skip this test if necessary. |
| 32 | + if _, err := os.Stat("/var/mqm/mqs.ini"); os.IsNotExist(err) { |
| 33 | + fmt.Println("Skipping local bindings test as there is no local queue manager installation.") |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + // Create a connection factory that indicates we should use local bindings. |
| 38 | + // Note that it includes none of the normal parameters like hostname, port etc. |
| 39 | + cf := mqjms.ConnectionFactoryImpl{ |
| 40 | + QMName: "QM1", |
| 41 | + TransportType: mqjms.TransportType_BINDINGS, |
| 42 | + } |
| 43 | + |
| 44 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 45 | + // at the end of the function (if it was created successfully) |
| 46 | + context, ctxErr := cf.CreateContext() |
| 47 | + assert.Nil(t, ctxErr) |
| 48 | + if context != nil { |
| 49 | + defer context.Close() |
| 50 | + } |
| 51 | + assert.NotNil(t, context) |
| 52 | + |
| 53 | + // Equivalent to a JNDI lookup or other declarative definition |
| 54 | + queue := context.CreateQueue("LOCAL.QUEUE") |
| 55 | + |
| 56 | + // Set up the consumer ready to receive messages. |
| 57 | + consumer, conErr := context.CreateConsumer(queue) |
| 58 | + assert.Nil(t, conErr) |
| 59 | + if consumer != nil { |
| 60 | + defer consumer.Close() |
| 61 | + } |
| 62 | + |
| 63 | + // Check no message on the queue to start with |
| 64 | + testMsg, err1 := consumer.ReceiveNoWait() |
| 65 | + assert.Nil(t, err1) |
| 66 | + assert.Nil(t, testMsg) |
| 67 | + |
| 68 | + // Check the getter/setter behaviour on a producer. |
| 69 | + tmpProducer := context.CreateProducer() |
| 70 | + tmpProducer.SetTimeToLive(2000) |
| 71 | + |
| 72 | + // Send a message that will expire after 1 second, then immediately receive it (will not have expired) |
| 73 | + msgBody := "Local message." |
| 74 | + context.CreateProducer().SendString(queue, msgBody) |
| 75 | + rcvBody, rcvErr := consumer.ReceiveStringBodyNoWait() |
| 76 | + assert.Nil(t, rcvErr) |
| 77 | + assert.NotNil(t, rcvBody) |
| 78 | + assert.Equal(t, msgBody, *rcvBody) |
| 79 | + |
| 80 | +} |
0 commit comments