|
1 | 1 | import * as chai from 'chai';
|
2 | 2 | import * as chaiAsPromised from 'chai-as-promised';
|
3 |
| -import { spy, restore } from 'simple-mock'; |
| 3 | +import { spy, restore, stub } from 'simple-mock'; |
4 | 4 | import { isAsyncIterable } from 'iterall';
|
5 | 5 | import { RedisPubSub } from '../redis-pubsub';
|
6 | 6 | import * as IORedis from 'ioredis';
|
@@ -272,6 +272,106 @@ describe('RedisPubSub', () => {
|
272 | 272 | });
|
273 | 273 | });
|
274 | 274 |
|
| 275 | + it('refuses custom reviver with a deserializer', done => { |
| 276 | + const reviver = stub(); |
| 277 | + const deserializer = stub(); |
| 278 | + |
| 279 | + try { |
| 280 | + expect(() => new RedisPubSub({...mockOptions, reviver, deserializer})) |
| 281 | + .to.throw("Reviver and deserializer can't be used together"); |
| 282 | + done(); |
| 283 | + } catch (e) { |
| 284 | + done(e); |
| 285 | + } |
| 286 | + }); |
| 287 | + |
| 288 | + it('allows to use a custom serializer', done => { |
| 289 | + const serializer = stub(); |
| 290 | + const serializedPayload = `{ "hello": "custom" }`; |
| 291 | + serializer.returnWith(serializedPayload); |
| 292 | + |
| 293 | + const pubSub = new RedisPubSub({...mockOptions, serializer }); |
| 294 | + |
| 295 | + try { |
| 296 | + pubSub.subscribe('TOPIC', message => { |
| 297 | + try { |
| 298 | + expect(message).to.eql({hello: 'custom'}); |
| 299 | + done(); |
| 300 | + } catch (e) { |
| 301 | + done(e); |
| 302 | + } |
| 303 | + }).then(() => { |
| 304 | + pubSub.publish('TOPIC', {hello: 'world'}); |
| 305 | + }); |
| 306 | + } catch (e) { |
| 307 | + done(e); |
| 308 | + } |
| 309 | + }); |
| 310 | + |
| 311 | + it('custom serializer can throw an error', done => { |
| 312 | + const serializer = stub(); |
| 313 | + serializer.throwWith(new Error('Custom serialization error')); |
| 314 | + |
| 315 | + const pubSub = new RedisPubSub({...mockOptions, serializer }); |
| 316 | + |
| 317 | + try { |
| 318 | + pubSub.publish('TOPIC', {hello: 'world'}).then(() => { |
| 319 | + done(new Error('Expected error to be thrown upon publish')); |
| 320 | + }, err => { |
| 321 | + expect(err.message).to.eql('Custom serialization error'); |
| 322 | + done(); |
| 323 | + }); |
| 324 | + } catch (e) { |
| 325 | + done(e); |
| 326 | + } |
| 327 | + }); |
| 328 | + |
| 329 | + it('allows to use a custom deserializer', done => { |
| 330 | + const deserializer = stub(); |
| 331 | + const deserializedPayload = { hello: 'custom' }; |
| 332 | + deserializer.returnWith(deserializedPayload); |
| 333 | + |
| 334 | + const pubSub = new RedisPubSub({...mockOptions, deserializer }); |
| 335 | + |
| 336 | + try { |
| 337 | + pubSub.subscribe('TOPIC', message => { |
| 338 | + try { |
| 339 | + expect(message).to.eql({hello: 'custom'}); |
| 340 | + done(); |
| 341 | + } catch (e) { |
| 342 | + done(e); |
| 343 | + } |
| 344 | + }).then(() => { |
| 345 | + pubSub.publish('TOPIC', {hello: 'world'}); |
| 346 | + }); |
| 347 | + } catch (e) { |
| 348 | + done(e); |
| 349 | + } |
| 350 | + }); |
| 351 | + |
| 352 | + it('unparsed payload is returned if custom deserializer throws an error', done => { |
| 353 | + const deserializer = stub(); |
| 354 | + deserializer.throwWith(new Error('Custom deserialization error')); |
| 355 | + |
| 356 | + const pubSub = new RedisPubSub({...mockOptions, deserializer }); |
| 357 | + |
| 358 | + try { |
| 359 | + pubSub.subscribe('TOPIC', message => { |
| 360 | + try { |
| 361 | + expect(message).to.be.a('string'); |
| 362 | + expect(message).to.eql('{"hello":"world"}'); |
| 363 | + done(); |
| 364 | + } catch (e) { |
| 365 | + done(e); |
| 366 | + } |
| 367 | + }).then(() => { |
| 368 | + pubSub.publish('TOPIC', {hello: 'world'}); |
| 369 | + }); |
| 370 | + } catch (e) { |
| 371 | + done(e); |
| 372 | + } |
| 373 | + }); |
| 374 | + |
275 | 375 | it('throws if you try to unsubscribe with an unknown id', () => {
|
276 | 376 | const pubSub = new RedisPubSub(mockOptions);
|
277 | 377 | return expect(() => pubSub.unsubscribe(123))
|
|
0 commit comments