go-micro.dev/v5@v5.12.0/broker/rabbitmq/connection_test.go (about)

     1  package rabbitmq
     2  
     3  import (
     4  	"crypto/tls"
     5  	"errors"
     6  	"testing"
     7  
     8  	amqp "github.com/rabbitmq/amqp091-go"
     9  	"go-micro.dev/v5/logger"
    10  )
    11  
    12  func TestNewRabbitMQConnURL(t *testing.T) {
    13  	testcases := []struct {
    14  		title string
    15  		urls  []string
    16  		want  string
    17  	}{
    18  		{"Multiple URLs", []string{"amqp://example.com/one", "amqp://example.com/two"}, "amqp://example.com/one"},
    19  		{"Insecure URL", []string{"amqp://example.com"}, "amqp://example.com"},
    20  		{"Secure URL", []string{"amqps://example.com"}, "amqps://example.com"},
    21  		{"Invalid URL", []string{"http://example.com"}, DefaultRabbitURL},
    22  		{"No URLs", []string{}, DefaultRabbitURL},
    23  	}
    24  
    25  	for _, test := range testcases {
    26  		conn := newRabbitMQConn(Exchange{Name: "exchange"}, test.urls, 0, false, false, false, logger.DefaultLogger)
    27  
    28  		if have, want := conn.url, test.want; have != want {
    29  			t.Errorf("%s: invalid url, want %q, have %q", test.title, want, have)
    30  		}
    31  	}
    32  }
    33  
    34  func TestTryToConnectTLS(t *testing.T) {
    35  	var (
    36  		dialCount, dialTLSCount int
    37  
    38  		err = errors.New("stop connect here")
    39  	)
    40  
    41  	dialConfig = func(_ string, c amqp.Config) (*amqp.Connection, error) {
    42  		if c.TLSClientConfig != nil {
    43  			dialTLSCount++
    44  			return nil, err
    45  		}
    46  
    47  		dialCount++
    48  		return nil, err
    49  	}
    50  
    51  	testcases := []struct {
    52  		title      string
    53  		url        string
    54  		secure     bool
    55  		amqpConfig *amqp.Config
    56  		wantTLS    bool
    57  	}{
    58  		{"unsecure url, secure false, no tls config", "amqp://example.com", false, nil, false},
    59  		{"secure url, secure false, no tls config", "amqps://example.com", false, nil, true},
    60  		{"unsecure url, secure true, no tls config", "amqp://example.com", true, nil, true},
    61  		{"unsecure url, secure false, tls config", "amqp://example.com", false, &amqp.Config{TLSClientConfig: &tls.Config{}}, true},
    62  	}
    63  
    64  	for _, test := range testcases {
    65  		dialCount, dialTLSCount = 0, 0
    66  
    67  		conn := newRabbitMQConn(Exchange{Name: "exchange"}, []string{test.url}, 0, false, false, false, logger.DefaultLogger)
    68  		conn.tryConnect(test.secure, test.amqpConfig)
    69  
    70  		have := dialCount
    71  		if test.wantTLS {
    72  			have = dialTLSCount
    73  		}
    74  
    75  		if have != 1 {
    76  			t.Errorf("%s: used wrong dialer, Dial called %d times, DialTLS called %d times", test.title, dialCount, dialTLSCount)
    77  		}
    78  	}
    79  }
    80  
    81  func TestNewRabbitMQPrefetchConfirmPublish(t *testing.T) {
    82  	testcases := []struct {
    83  		title          string
    84  		urls           []string
    85  		prefetchCount  int
    86  		prefetchGlobal bool
    87  		confirmPublish bool
    88  	}{
    89  		{"Multiple URLs", []string{"amqp://example.com/one", "amqp://example.com/two"}, 1, true, true},
    90  		{"Insecure URL", []string{"amqp://example.com"}, 1, true, true},
    91  		{"Secure URL", []string{"amqps://example.com"}, 1, true, true},
    92  		{"Invalid URL", []string{"http://example.com"}, 1, true, true},
    93  		{"No URLs", []string{}, 1, true, true},
    94  	}
    95  
    96  	for _, test := range testcases {
    97  		conn := newRabbitMQConn(Exchange{Name: "exchange"}, test.urls, test.prefetchCount, test.prefetchGlobal, test.confirmPublish, false, logger.DefaultLogger)
    98  
    99  		if have, want := conn.prefetchCount, test.prefetchCount; have != want {
   100  			t.Errorf("%s: invalid prefetch count, want %d, have %d", test.title, want, have)
   101  		}
   102  
   103  		if have, want := conn.prefetchGlobal, test.prefetchGlobal; have != want {
   104  			t.Errorf("%s: invalid prefetch global setting, want %t, have %t", test.title, want, have)
   105  		}
   106  
   107  		if have, want := conn.confirmPublish, test.confirmPublish; have != want {
   108  			t.Errorf("%s: invalid confirm setting, want %t, have %t", test.title, want, have)
   109  		}
   110  	}
   111  }