|
| 1 | +package mqttv3_paho |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +func TestWithSubscribeMap(t *testing.T) { |
| 10 | + t.Run("should do nothing for nil map", func(t *testing.T) { |
| 11 | + p := &Protocol{subscriptions: map[string]byte{"existing/topic": 1}} |
| 12 | + err := WithSubscribeMap(nil)(p) |
| 13 | + |
| 14 | + require.NoError(t, err) |
| 15 | + require.Equal(t, map[string]byte{"existing/topic": 1}, p.subscriptions) |
| 16 | + }) |
| 17 | + |
| 18 | + t.Run("should do nothing for nil protocol", func(t *testing.T) { |
| 19 | + err := WithSubscribeMap(map[string]byte{"test/topic": 1})(nil) |
| 20 | + |
| 21 | + require.NoError(t, err) |
| 22 | + }) |
| 23 | + |
| 24 | + t.Run("should add subscriptions", func(t *testing.T) { |
| 25 | + p := &Protocol{} |
| 26 | + err := WithSubscribeMap(map[string]byte{"test/topic": 1, "another/topic": 0})(p) |
| 27 | + |
| 28 | + require.NoError(t, err) |
| 29 | + require.Equal(t, map[string]byte{"test/topic": 1, "another/topic": 0}, p.subscriptions) |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +func TestWithSubscribeTopic(t *testing.T) { |
| 34 | + t.Run("should do nothing for empty topic", func(t *testing.T) { |
| 35 | + p := &Protocol{subscriptions: map[string]byte{"existing/topic": 1}} |
| 36 | + err := WithSubscribeTopic("", 1)(p) |
| 37 | + |
| 38 | + require.NoError(t, err) |
| 39 | + require.Equal(t, map[string]byte{"existing/topic": 1}, p.subscriptions) |
| 40 | + }) |
| 41 | + |
| 42 | + t.Run("should do nothing for nil protocol", func(t *testing.T) { |
| 43 | + err := WithSubscribeTopic("test/topic", 1)(nil) |
| 44 | + |
| 45 | + require.NoError(t, err) |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("should add subscription", func(t *testing.T) { |
| 49 | + p := &Protocol{} |
| 50 | + err := WithSubscribeTopic("test/topic", 1)(p) |
| 51 | + |
| 52 | + require.NoError(t, err) |
| 53 | + require.Equal(t, map[string]byte{"test/topic": 1}, p.subscriptions) |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +func TestWithDisconnectQuiesce(t *testing.T) { |
| 58 | + t.Run("should do nothing for zero quiesce", func(t *testing.T) { |
| 59 | + p := &Protocol{quiesce: 5} |
| 60 | + err := WithDisconnectQuiesce(0)(p) |
| 61 | + |
| 62 | + require.NoError(t, err) |
| 63 | + require.Equal(t, uint(5), p.quiesce) |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("should do nothing for nil protocol", func(t *testing.T) { |
| 67 | + err := WithDisconnectQuiesce(10)(nil) |
| 68 | + |
| 69 | + require.NoError(t, err) |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("should set quiesce", func(t *testing.T) { |
| 73 | + p := &Protocol{} |
| 74 | + err := WithDisconnectQuiesce(10)(p) |
| 75 | + |
| 76 | + require.NoError(t, err) |
| 77 | + require.Equal(t, uint(10), p.quiesce) |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +func TestWithPublishTopic(t *testing.T) { |
| 82 | + t.Run("should not set empty topic", func(t *testing.T) { |
| 83 | + p := &Protocol{} |
| 84 | + err := WithPublishTopic("", 1, true)(p) |
| 85 | + |
| 86 | + require.NoError(t, err) |
| 87 | + require.Equal(t, "", p.topic) |
| 88 | + require.Equal(t, byte(0), p.qos) |
| 89 | + require.Equal(t, false, p.retained) |
| 90 | + }) |
| 91 | + |
| 92 | + t.Run("should do nothing for nil protocol", func(t *testing.T) { |
| 93 | + err := WithPublishTopic("test/topic", 1, true)(nil) |
| 94 | + |
| 95 | + require.NoError(t, err) |
| 96 | + }) |
| 97 | + |
| 98 | + t.Run("should set topic", func(t *testing.T) { |
| 99 | + p := &Protocol{} |
| 100 | + err := WithPublishTopic("test/topic", 1, true)(p) |
| 101 | + |
| 102 | + require.NoError(t, err) |
| 103 | + require.Equal(t, "test/topic", p.topic) |
| 104 | + require.Equal(t, byte(1), p.qos) |
| 105 | + require.Equal(t, true, p.retained) |
| 106 | + }) |
| 107 | +} |
0 commit comments