|
| 1 | +import { Configuration } from '../configuration/Configuration'; |
| 2 | +import { ContextData } from 'ContextData'; |
| 3 | +import { ExceptionlessClient } from '../ExceptionlessClient'; |
| 4 | +import { EventPluginManager } from 'EventPluginManager'; |
| 5 | +import { EventPluginContext } from 'EventPluginContext'; |
| 6 | + |
| 7 | +describe('EventPluginManager', () => { |
| 8 | + it('should add items to the event.', (done) => { |
| 9 | + var client = new ExceptionlessClient({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'}); |
| 10 | + var context = new EventPluginContext(client, {}, new ContextData()); |
| 11 | + expect(context.event.source).toBeUndefined(); |
| 12 | + expect(context.event.geo).toBeUndefined(); |
| 13 | + |
| 14 | + expect(client.config.plugins).not.toBe(null); |
| 15 | + while(client.config.plugins.length > 0) { |
| 16 | + client.config.removePlugin(client.config.plugins[0]); |
| 17 | + } |
| 18 | + |
| 19 | + client.config.addPlugin('1', 1, (context:EventPluginContext, next?:() => void) => { |
| 20 | + setTimeout(() => { |
| 21 | + context.event.source = 'plugin 1'; |
| 22 | + |
| 23 | + if (next) { |
| 24 | + next(); |
| 25 | + } |
| 26 | + }, 500); |
| 27 | + }); |
| 28 | + |
| 29 | + client.config.addPlugin('2', 2, (context:EventPluginContext, next?:() => void) => { |
| 30 | + context.event.geo = '43.5775,88.4472'; |
| 31 | + |
| 32 | + if (next) { |
| 33 | + next(); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + EventPluginManager.run(context, (context?:EventPluginContext) => { |
| 38 | + expect(context.cancelled).toBeUndefined(); |
| 39 | + expect(context.event.source).toBe('plugin 1'); |
| 40 | + expect(context.event.geo).toBe('43.5775,88.4472'); |
| 41 | + |
| 42 | + done(); |
| 43 | + }); |
| 44 | + }, 5000); |
| 45 | + |
| 46 | + it('setting cancel should stop plugin execution.', (done) => { |
| 47 | + var client = new ExceptionlessClient({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'}); |
| 48 | + var context = new EventPluginContext(client, {}, new ContextData()); |
| 49 | + |
| 50 | + expect(client.config.plugins).not.toBe(null); |
| 51 | + while(client.config.plugins.length > 0) { |
| 52 | + client.config.removePlugin(client.config.plugins[0]); |
| 53 | + } |
| 54 | + |
| 55 | + client.config.addPlugin('1', 1, (context:EventPluginContext, next?:() => void) => { |
| 56 | + context.cancelled = true; |
| 57 | + |
| 58 | + if (next) { |
| 59 | + next(); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + client.config.addPlugin('2', 2, () => { |
| 64 | + // Fail this test as this plugin should not be called. |
| 65 | + expect(false).toBe(true); |
| 66 | + }); |
| 67 | + |
| 68 | + EventPluginManager.run(context, (context?:EventPluginContext) => { |
| 69 | + expect(context.cancelled).toBe(true); |
| 70 | + done(); |
| 71 | + }); |
| 72 | + }, 5000); |
| 73 | + |
| 74 | + it('throwing error should stop plugin execution.', (done) => { |
| 75 | + var client = new ExceptionlessClient({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'}); |
| 76 | + var context = new EventPluginContext(client, {}, new ContextData()); |
| 77 | + |
| 78 | + expect(client.config.plugins).not.toBe(null); |
| 79 | + while(client.config.plugins.length > 0) { |
| 80 | + client.config.removePlugin(client.config.plugins[0]); |
| 81 | + } |
| 82 | + |
| 83 | + client.config.addPlugin('1', 1, () => { |
| 84 | + throw new Error('Random Error'); |
| 85 | + }); |
| 86 | + |
| 87 | + client.config.addPlugin('2', 2, () => { |
| 88 | + // Fail this test as this plugin should not be called. |
| 89 | + expect(false).toBe(true); |
| 90 | + }); |
| 91 | + |
| 92 | + EventPluginManager.run(context, (context?:EventPluginContext) => { |
| 93 | + expect(context.cancelled).toBe(true); |
| 94 | + done(); |
| 95 | + }); |
| 96 | + }, 5000); |
| 97 | + |
| 98 | + it('throwing async error should stop plugin execution.', (done) => { |
| 99 | + var client = new ExceptionlessClient({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'}); |
| 100 | + var context = new EventPluginContext(client, {}, new ContextData()); |
| 101 | + |
| 102 | + expect(client.config.plugins).not.toBe(null); |
| 103 | + while(client.config.plugins.length > 0) { |
| 104 | + client.config.removePlugin(client.config.plugins[0]); |
| 105 | + } |
| 106 | + |
| 107 | + client.config.addPlugin('1', 1, (context:EventPluginContext, next?:() => void) => { |
| 108 | + // NOTE: Currently you have to catch it and set cancelled. |
| 109 | + setTimeout(() => { |
| 110 | + try { |
| 111 | + throw new Error('Random Error'); |
| 112 | + } catch (e) { |
| 113 | + context.cancelled = true; |
| 114 | + } |
| 115 | + |
| 116 | + if (next) { |
| 117 | + next(); |
| 118 | + } |
| 119 | + }, 500); |
| 120 | + }); |
| 121 | + |
| 122 | + client.config.addPlugin('2', 2, () => { |
| 123 | + // Fail this test as this plugin should not be called. |
| 124 | + expect(false).toBe(true); |
| 125 | + }); |
| 126 | + |
| 127 | + EventPluginManager.run(context, (context?:EventPluginContext) => { |
| 128 | + expect(context.cancelled).toBe(true); |
| 129 | + done(); |
| 130 | + }); |
| 131 | + }, 5000); |
| 132 | + |
| 133 | + it('should cancel via timeout.', (done) => { |
| 134 | + var client = new ExceptionlessClient({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'}); |
| 135 | + var context = new EventPluginContext(client, {}, new ContextData()); |
| 136 | + |
| 137 | + expect(client.config.plugins).not.toBe(null); |
| 138 | + while(client.config.plugins.length > 0) { |
| 139 | + client.config.removePlugin(client.config.plugins[0]); |
| 140 | + } |
| 141 | + |
| 142 | + client.config.addPlugin('1', 1, (context:EventPluginContext, next?:() => void) => { |
| 143 | + setTimeout(done, 100) |
| 144 | + }); |
| 145 | + |
| 146 | + client.config.addPlugin('2', 2, () => { |
| 147 | + // Fail this test as this plugin should not be called. |
| 148 | + expect(false).toBe(true); |
| 149 | + }); |
| 150 | + |
| 151 | + EventPluginManager.run(context, (context?:EventPluginContext) => { |
| 152 | + // Fail this test as this callback should not be called. |
| 153 | + expect(false).toBe(true); |
| 154 | + }); |
| 155 | + }, 500); |
| 156 | +}); |
0 commit comments