Skip to content

Commit ca6e2a9

Browse files
committed
[broker-25] continue working on mock client
1 parent 1f6d2e8 commit ca6e2a9

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

src/main/java/com/ss/mqtt/broker/network/packet/out/Connect311OutPacket.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ public class Connect311OutPacket extends MqttWritablePacket {
3232
private final boolean willRetain;
3333
private final boolean cleanStart;
3434

35+
public Connect311OutPacket(@NotNull String clientId, int keepAlive) {
36+
this(
37+
StringUtils.EMPTY,
38+
StringUtils.EMPTY,
39+
clientId,
40+
ArrayUtils.EMPTY_BYTE_ARRAY,
41+
ArrayUtils.EMPTY_BYTE_ARRAY,
42+
QoS.AT_MOST_ONCE_DELIVERY,
43+
keepAlive,
44+
false,
45+
false
46+
);
47+
}
48+
3549
protected @NotNull MqttVersion getMqttVersion() {
3650
return MqttVersion.MQTT_3_1_1;
3751
}

src/test/groovy/com/ss/mqtt/broker/test/integration/IntegrationSpecification.groovy

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class IntegrationSpecification extends Specification {
1919
public static final topicFilter = "topic/Filter"
2020
public static final publishPayload = "publishPayload".getBytes(encoding)
2121
public static final clientId = "testClientId"
22+
public static final keepAlive = 120
2223

2324
@Autowired
2425
InetSocketAddress deviceNetworkAddress
@@ -33,9 +34,9 @@ class IntegrationSpecification extends Specification {
3334
MqttConnection mqtt311MockedConnection
3435

3536
def buildClient() {
36-
return buildClient(UUID.randomUUID().toString())
37+
return buildClient(generateClientId())
3738
}
38-
39+
3940
def buildClient(String clientId) {
4041
return MqttClient.builder()
4142
.identifier(clientId)
@@ -45,8 +46,12 @@ class IntegrationSpecification extends Specification {
4546
.build()
4647
.toAsync()
4748
}
48-
49-
def static connectWith(Mqtt5AsyncClient client, String user, String pass) {
49+
50+
def generateClientId() {
51+
UUID.randomUUID().toString()
52+
}
53+
54+
def connectWith(Mqtt5AsyncClient client, String user, String pass) {
5055
return client.connectWith()
5156
.simpleAuth()
5257
.username(user)
Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
11
package com.ss.mqtt.broker.test.integration
22

3-
import com.ss.mqtt.broker.model.MqttSession
4-
import com.ss.mqtt.broker.network.client.MqttClient
5-
import com.ss.mqtt.broker.network.packet.in.PublishInPacket
6-
import com.ss.mqtt.broker.network.packet.out.ConnectAck5OutPacket
3+
import com.ss.mqtt.broker.network.packet.in.ConnectAckInPacket
4+
import com.ss.mqtt.broker.network.packet.out.Connect311OutPacket
75
import com.ss.mqtt.broker.service.MqttSessionService
86
import org.springframework.beans.factory.annotation.Autowired
97

10-
import java.util.concurrent.atomic.AtomicInteger
11-
128
class PublishRetryTest extends IntegrationSpecification {
139

1410
@Autowired
1511
MqttSessionService mqttSessionService
1612

17-
/* def "mqtt5 client should be generate session with one pending packet"() {
13+
def "mqtt5 client should be generate session with one pending packet"() {
1814
given:
19-
def client = buildMqtt5MockClient()
15+
def client = buildMqtt311MockClient()
16+
def clientId = generateClientId()
2017
when:
2118
client.connect()
22-
client.send(new ConnectAck5OutPacket(
23-
24-
))
25-
then:
26-
noExceptionThrown()
27-
publishRetryService.exist(identifier.get().toString())
28-
when:
29-
client.disconnect().join()
30-
Thread.sleep(500)
19+
client.send(new Connect311OutPacket(clientId, keepAlive))
3120
then:
32-
noExceptionThrown()
33-
!publishRetryService.exist(identifier.get().toString())
34-
}*/
21+
client.readNext() instanceof ConnectAckInPacket
22+
}
3523
}

src/test/groovy/com/ss/mqtt/broker/test/mock/MqttMockClient.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.ss.mqtt.broker.test.mock
22

33
import com.ss.mqtt.broker.network.MqttConnection
44
import com.ss.mqtt.broker.network.packet.PacketType
5+
import com.ss.mqtt.broker.network.packet.in.ConnectAckInPacket
56
import com.ss.mqtt.broker.network.packet.in.ConnectInPacket
67
import com.ss.mqtt.broker.network.packet.in.MqttReadablePacket
78
import com.ss.mqtt.broker.network.packet.out.MqttWritablePacket
@@ -72,8 +73,8 @@ class MqttMockClient {
7273
MqttReadablePacket packet
7374

7475
switch (PacketType.fromByte(type)) {
75-
case PacketType.CONNECT:
76-
packet = new ConnectInPacket(info)
76+
case PacketType.CONNECT_ACK:
77+
packet = new ConnectAckInPacket(info)
7778
break
7879
default:
7980
throw new IllegalStateException("Unknown packet of type: $type")

0 commit comments

Comments
 (0)