On Mon, Nov 18, 2019 at 10:00 PM Shane H <shane....@gmail.com> wrote: > > I'm trying to unit test some code (pasted below). I've struggled to find a > way to mock the amqp.Connection, so have decided to go the monkey patching > route. > > The test 'works' but only if I use the following incantation > go test -gcflags=-l > > So, my next step is to ensure that -gcflags is set to prevent inlining when > this test is executed. > My /thought/ was to check os.Getenv("GO_GCFLAGS"), but as I was kindly > reminded, executng the tests and compiling the tests are two different things. > My next brilliant idea was to use a build tag to ensure that the test file is > only built when the flags are set as I desire. But > https://golang.org/pkg/go/build/ doesn't offer any ideas where gcflags might > be checked, or what tag to use. > > So, I throw myself at the mercy of golang-nuts to (gently) point me in the > right direction (I completely understand that there may be an easier way to > do this, but I cannot see that path)
This is what I usually do in these situations: var amqpDial=amqp.Dial func (mq *MQ) Connect() (err error) { ... mq.conn, err = amqpDial(mq.URI) ... } func TestConnect(t *testing.T) { amqpDial=fakeDial defer func() {amqpDial=amqp.Dial}() ... } > > ``` > ### Code to test > // MQ - > type MQ struct { > conn *amqp.Connection > Retry int > URI string > } > > // Connect - > func (mq *MQ) Connect() (err error) { > // Retry MUST be > 0 > if mq.Retry == 0 { > log.Print("Cannot use a Retry of zero, this process will to default retry to > 1") > mq.Retry = 1 > } > > // Note: Even though amqp.ParseURI(uri) will validate the URI formed, check > here that the minimum required exists > if mq.URI == "" { > log.Printf("No Message Queue URI configured") > } > > for { > for i := 0; i < mq.Retry; i++ { > mq.conn, err = amqp.Dial(mq.URI) > if err == nil { > // Successful connection > log.Printf("Successfully connected to RabbitMQ") > return nil > } > time.Sleep(1 * time.Second) > } > // Log that there is a problem connecting to the RabbitMQ service that needs > urgent attention > backoff := time.Duration(mq.Retry*rand.Intn(10)) * time.Second > log.Printf("ALERT: Trouble connecting to RabbitMQ, error: %v, going to > re-enter retry loop in %s seconds", err, backoff.String()) > time.Sleep(backoff) > } > } > > ### Test Code > func TestConnect(t *testing.T) { > testcases := map[string]struct { > retry int > uri string > err error > }{ > "Happy Path": {retry: 1, uri: "amqp://localhost:5672/%2f"}, > } > for name, tc := range testcases { > t.Run(name, func(t *testing.T) { > // Monkeypatch amqp to return the nil and the error > fakeRabbitConnection := func(msg string) (*amqp.Connection, error) { > return nil, tc.err // I only want the error to have meaning, therefore the > connection can be nil (which also saves me having to create a mock) > } > patch := monkey.Patch(amqp.Dial, fakeRabbitConnection) > defer patch.Unpatch() > mq := rabbit.MQ{Retry: tc.retry, URI: tc.uri} > output := mq.Connect() > fmt.Println(output) // TODO > }) > } > } > ``` > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/133174cd-e5ef-4e65-bfe0-e626914803bb%40googlegroups.com. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAMV2RqqEp_T%2BM6qtu4Fw8ff%2BNRMq0QDtSOsYKUbmz-k72_U%3DWA%40mail.gmail.com.