|
38 | 38 | import static org.testng.Assert.assertTrue; |
39 | 39 |
|
40 | 40 | import org.apache.bifromq.mqtt.MockableTest; |
| 41 | +import org.apache.bifromq.mqtt.session.IMQTTSession; |
41 | 42 | import org.apache.bifromq.mqtt.session.IMQTTTransientSession; |
42 | 43 | import org.apache.bifromq.plugin.subbroker.CheckReply; |
43 | 44 | import org.apache.bifromq.plugin.subbroker.DeliveryPack; |
@@ -158,6 +159,7 @@ public void checkMatchInfoForNonSharedSub() { |
158 | 159 | assertEquals(code, CheckReply.Code.NO_RECEIVER); |
159 | 160 |
|
160 | 161 | when(localRoutes.routesInfo()).thenReturn(Map.of(channelId, 1L)); |
| 162 | + when(localSessionRegistry.get(channelId)).thenReturn(session); |
161 | 163 | code = localDistService.checkMatchInfo(tenantId, MatchInfo.newBuilder() |
162 | 164 | .setMatcher(TopicUtil.from(topicFilter)) |
163 | 165 | .setReceiverId(ILocalDistService.localize(channelId)) |
@@ -364,7 +366,7 @@ public void deliverToMismatchedReceiver() { |
364 | 366 |
|
365 | 367 | DeliveryResults results = reply.getResultMap().get(tenantId); |
366 | 368 | DeliveryResult result = results.getResult(0); |
367 | | - assertEquals(DeliveryResult.Code.NO_SUB, result.getCode()); |
| 369 | + assertEquals(result.getCode(), DeliveryResult.Code.NO_RECEIVER); |
368 | 370 | } |
369 | 371 |
|
370 | 372 | @Test |
@@ -398,7 +400,111 @@ public void deliverToNoLocalRoute() { |
398 | 400 |
|
399 | 401 | DeliveryResults results = reply.getResultMap().get(tenantId); |
400 | 402 | DeliveryResult result = results.getResult(0); |
401 | | - assertEquals(DeliveryResult.Code.NO_SUB, result.getCode()); |
| 403 | + assertEquals(result.getCode(), DeliveryResult.Code.NO_RECEIVER); |
| 404 | + } |
| 405 | + |
| 406 | + @Test |
| 407 | + public void deliverToEmptyLocalRoutes() { |
| 408 | + String tenantId = "tenant1"; |
| 409 | + String topic = "testTopic"; |
| 410 | + String topicFilter = "testTopic/#"; |
| 411 | + String channelId = "channel0"; |
| 412 | + MatchInfo matchInfo = MatchInfo.newBuilder() |
| 413 | + .setMatcher(TopicUtil.from(topicFilter)) |
| 414 | + .setReceiverId("receiverId") |
| 415 | + .build(); |
| 416 | + TopicMessagePack topicMessagePack = TopicMessagePack.newBuilder().setTopic(topic).build(); |
| 417 | + DeliveryPackage deliveryPack = DeliveryPackage.newBuilder() |
| 418 | + .addPack(DeliveryPack.newBuilder().setMessagePack(topicMessagePack).addMatchInfo(matchInfo).build()) |
| 419 | + .build(); |
| 420 | + DeliveryRequest request = DeliveryRequest.newBuilder().putPackage(tenantId, deliveryPack).build(); |
| 421 | + |
| 422 | + ILocalTopicRouter.ILocalRoutes localRoutes = mock(ILocalTopicRouter.ILocalRoutes.class); |
| 423 | + when(localRoutes.localReceiverId()).thenReturn("receiverId"); |
| 424 | + when(localRoutes.routesInfo()).thenReturn(Map.of()); |
| 425 | + when(localTopicRouter.getTopicRoutes(anyString(), any())).thenReturn( |
| 426 | + Optional.of(CompletableFuture.completedFuture(localRoutes))); |
| 427 | + |
| 428 | + LocalDistService localDistService = |
| 429 | + new LocalDistService(serverId, localSessionRegistry, localTopicRouter, distClient, resourceThrottler); |
| 430 | + |
| 431 | + CompletableFuture<DeliveryReply> future = localDistService.dist(request); |
| 432 | + DeliveryReply reply = future.join(); |
| 433 | + |
| 434 | + DeliveryResults results = reply.getResultMap().get(tenantId); |
| 435 | + DeliveryResult result = results.getResult(0); |
| 436 | + assertEquals(result.getCode(), DeliveryResult.Code.NO_RECEIVER); |
| 437 | + } |
| 438 | + |
| 439 | + @Test |
| 440 | + public void deliverToNoLocalSession() { |
| 441 | + String tenantId = "tenant1"; |
| 442 | + String topic = "testTopic"; |
| 443 | + String topicFilter = "testTopic/#"; |
| 444 | + String channelId = "channel0"; |
| 445 | + MatchInfo matchInfo = MatchInfo.newBuilder() |
| 446 | + .setMatcher(TopicUtil.from(topicFilter)) |
| 447 | + .setReceiverId("receiverId") |
| 448 | + .build(); |
| 449 | + TopicMessagePack topicMessagePack = TopicMessagePack.newBuilder().setTopic(topic).build(); |
| 450 | + DeliveryPackage deliveryPack = DeliveryPackage.newBuilder() |
| 451 | + .addPack(DeliveryPack.newBuilder().setMessagePack(topicMessagePack).addMatchInfo(matchInfo).build()) |
| 452 | + .build(); |
| 453 | + DeliveryRequest request = DeliveryRequest.newBuilder().putPackage(tenantId, deliveryPack).build(); |
| 454 | + |
| 455 | + ILocalTopicRouter.ILocalRoutes localRoutes = mock(ILocalTopicRouter.ILocalRoutes.class); |
| 456 | + when(localRoutes.localReceiverId()).thenReturn("receiverId"); |
| 457 | + when(localRoutes.routesInfo()).thenReturn(Map.of(channelId, 1L)); |
| 458 | + when(localTopicRouter.getTopicRoutes(anyString(), any())).thenReturn( |
| 459 | + Optional.of(CompletableFuture.completedFuture(localRoutes))); |
| 460 | + |
| 461 | + when(localSessionRegistry.get(channelId)).thenReturn(null); |
| 462 | + |
| 463 | + LocalDistService localDistService = |
| 464 | + new LocalDistService(serverId, localSessionRegistry, localTopicRouter, distClient, resourceThrottler); |
| 465 | + |
| 466 | + CompletableFuture<DeliveryReply> future = localDistService.dist(request); |
| 467 | + DeliveryReply reply = future.join(); |
| 468 | + |
| 469 | + DeliveryResults results = reply.getResultMap().get(tenantId); |
| 470 | + DeliveryResult result = results.getResult(0); |
| 471 | + assertEquals(result.getCode(), DeliveryResult.Code.NO_RECEIVER); |
| 472 | + } |
| 473 | + |
| 474 | + @Test |
| 475 | + public void deliverToNonTransientSession() { |
| 476 | + String tenantId = "tenant1"; |
| 477 | + String topic = "testTopic"; |
| 478 | + String topicFilter = "testTopic/#"; |
| 479 | + String channelId = "channel0"; |
| 480 | + MatchInfo matchInfo = MatchInfo.newBuilder() |
| 481 | + .setMatcher(TopicUtil.from(topicFilter)) |
| 482 | + .setReceiverId("receiverId") |
| 483 | + .build(); |
| 484 | + TopicMessagePack topicMessagePack = TopicMessagePack.newBuilder().setTopic(topic).build(); |
| 485 | + DeliveryPackage deliveryPack = DeliveryPackage.newBuilder() |
| 486 | + .addPack(DeliveryPack.newBuilder().setMessagePack(topicMessagePack).addMatchInfo(matchInfo).build()) |
| 487 | + .build(); |
| 488 | + DeliveryRequest request = DeliveryRequest.newBuilder().putPackage(tenantId, deliveryPack).build(); |
| 489 | + |
| 490 | + ILocalTopicRouter.ILocalRoutes localRoutes = mock(ILocalTopicRouter.ILocalRoutes.class); |
| 491 | + when(localRoutes.localReceiverId()).thenReturn("receiverId"); |
| 492 | + when(localRoutes.routesInfo()).thenReturn(Map.of(channelId, 1L)); |
| 493 | + when(localTopicRouter.getTopicRoutes(anyString(), any())).thenReturn( |
| 494 | + Optional.of(CompletableFuture.completedFuture(localRoutes))); |
| 495 | + |
| 496 | + IMQTTSession nonTransientSession = mock(IMQTTSession.class); |
| 497 | + when(localSessionRegistry.get(channelId)).thenReturn(nonTransientSession); |
| 498 | + |
| 499 | + LocalDistService localDistService = |
| 500 | + new LocalDistService(serverId, localSessionRegistry, localTopicRouter, distClient, resourceThrottler); |
| 501 | + |
| 502 | + CompletableFuture<DeliveryReply> future = localDistService.dist(request); |
| 503 | + DeliveryReply reply = future.join(); |
| 504 | + |
| 505 | + DeliveryResults results = reply.getResultMap().get(tenantId); |
| 506 | + DeliveryResult result = results.getResult(0); |
| 507 | + assertEquals(result.getCode(), DeliveryResult.Code.NO_RECEIVER); |
402 | 508 | } |
403 | 509 |
|
404 | 510 | @Test |
@@ -432,7 +538,7 @@ public void deliverToNoResolvedRoute() { |
432 | 538 |
|
433 | 539 | DeliveryResults results = reply.getResultMap().get(tenantId); |
434 | 540 | DeliveryResult result = results.getResult(0); |
435 | | - assertEquals(DeliveryResult.Code.OK, result.getCode()); |
| 541 | + assertEquals(result.getCode(), DeliveryResult.Code.OK); |
436 | 542 | } |
437 | 543 |
|
438 | 544 | @Test |
@@ -467,7 +573,7 @@ public void deliverWhileRouteResolveException() { |
467 | 573 |
|
468 | 574 | DeliveryResults results = reply.getResultMap().get(tenantId); |
469 | 575 | DeliveryResult result = results.getResult(0); |
470 | | - assertEquals(DeliveryResult.Code.OK, result.getCode()); |
| 576 | + assertEquals(result.getCode(), DeliveryResult.Code.OK); |
471 | 577 | } |
472 | 578 |
|
473 | 579 | @Test |
|
0 commit comments