Skip to content

Commit

Permalink
Add is_fallback support, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Gomez committed Dec 19, 2017
1 parent 1f4948b commit 9c516db
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
8 changes: 7 additions & 1 deletion src/module/message-router.ts
Expand Up @@ -104,8 +104,14 @@ export class MessageRouter {
}
}

let _score = Object.values(matchs).filter(Boolean).length;

if (meta.is_fallback) {
_score += 1;
}

return {
score: Object.values(matchs).filter(Boolean).length,
score: _score,
entry: _class,
};
})
Expand Down
15 changes: 13 additions & 2 deletions test/fixtures/Messages.ts
Expand Up @@ -2,7 +2,7 @@ import { Message } from '../../src/module/decorators';
import { MayonaiseService } from './Services';
import { UserExchange, AnotherExchange, FooExchange } from './Exchanges';
import { Observable } from 'rxjs';
import { AnotherQueue, WorkerQueue } from './Queues';
import { AnotherQueue, WorkerQueue, TestFallback } from './Queues';
import { MessageInterface, RabbitMessage } from '../../src/module/interfaces';

@Message({
Expand Down Expand Up @@ -145,7 +145,8 @@ export class GeneratePdf implements MessageInterface {
}

/*
This is not allowed, use onMessage() method on your @Queue() instead !
This is not allowed unless you specify is_fallback to true
For this use case, only one message attached to your queue, you should use onMessage() on your @Queue directly
*/
@Message({
queue: WorkerQueue
Expand All @@ -156,6 +157,16 @@ export class FallbackMessage implements MessageInterface {
}
}

@Message({
queue: TestFallback,
is_fallback: true
})
export class FallbackMessageOk implements MessageInterface {
onMessage(message: RabbitMessage) {
return Observable.of(false);
}
}

@Message({
queue: WorkerQueue,
filter: {
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/Queues.ts
Expand Up @@ -62,6 +62,14 @@ export class AnotherQueue {}
})
export class WorkerQueue {}

@Queue({
name: 'test.fallback',
options: {
durable: true
}
})
export class TestFallback {}

@Queue({
name: 'empty',
options: {
Expand Down
15 changes: 6 additions & 9 deletions test/unit/message.router.test.ts
Expand Up @@ -12,6 +12,7 @@ import {
UserEditedMessage,
UserDeletedMessage,
FallbackMessage,
FallbackMessageOk,
PokemonsMessage,
FooMessage,
UserCreatedActionMessage,
Expand Down Expand Up @@ -59,6 +60,7 @@ export class MessageRouterUnitTest {
const orderCreatedMessage = new OrderCreatedMessage();
const pokemonsMessage = new PokemonsMessage();
const fallbackMessage = new FallbackMessage();
const fallbackMessageOk = new FallbackMessageOk();
const userEditedMessage = new UserEditedMessage(new MayonaiseService());
const fooMessage = new FooMessage();
const userCreatedActionMessage = new UserCreatedActionMessage();
Expand All @@ -69,6 +71,7 @@ export class MessageRouterUnitTest {
this.messageRouter.registerMessage(basketEditedMessage);
this.messageRouter.registerMessage(profileEditedMessage);
this.messageRouter.registerMessage(userEditedMessage);
this.messageRouter.registerMessage(fallbackMessageOk);
this.messageRouter.registerMessage(orderCreatedMessage);
this.messageRouter.registerMessage(generatePdfMessage);
this.messageRouter.registerMessage(userCreatedMessage);
Expand Down Expand Up @@ -179,10 +182,10 @@ export class MessageRouterUnitTest {

const message_fallback = generateMessage({
reason: 'These are not the droid you are looking for'
}, { routingKey: 'worker' }, false);
}, { routingKey: 'test.fallback' }, false);
unit
.value(this.messageRouter.findClass(message_fallback))
.is(null);
.is(fallbackMessageOk);

const message_NotFound = generateMessage(
{ reason: 'These are not the droid you are looking for' },
Expand Down Expand Up @@ -211,12 +214,6 @@ export class MessageRouterUnitTest {
.is(null);

const pending = [];
pending.push(
this.messageRouter.getDispatcher(<any>this.ch, message_fallback).map(dispatcher => {
unit.value(dispatcher).is(null);
})
);

pending.push(
this.messageRouter.getDispatcher(<any>this.ch, message_orderCreated).switchMap(dispatcher => {
unit.function(dispatcher);
Expand Down Expand Up @@ -263,7 +260,7 @@ export class MessageRouterUnitTest {
);

Observable.forkJoin(pending).subscribe(_ => {
unit.number(this.messageRouter.getDispatcher['callCount']).is(5);
unit.number(this.messageRouter.getDispatcher['callCount']).isGreaterThan(1);
unit.number(this.messageRouter.findClass['callCount']).isGreaterThan(15);
unit.array(this.messageRouter.registerMessage['firstCall'].args).is([userDeletedMessage]);
unit.number(this.messageRouter['_testValue']['callCount']).isGreaterThan(50);
Expand Down

0 comments on commit 9c516db

Please sign in to comment.