|
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | +using iText.IO.Util; |
| 4 | +using iText.Kernel; |
| 5 | +using iText.Kernel.Counter.Data; |
| 6 | +using iText.Kernel.Counter.Event; |
| 7 | +using iText.Test; |
| 8 | +using iText.Test.Attributes; |
| 9 | + |
| 10 | +namespace iText.Kernel.Counter { |
| 11 | + public class DataHandlerCounterTest : ExtendedITextTest { |
| 12 | + [NUnit.Framework.Test] |
| 13 | + public virtual void DisableHooksTest() { |
| 14 | + int betweenChecksSleepTime = 500; |
| 15 | + int handlerSleepTime = 100; |
| 16 | + DataHandlerCounterTest.TestDataHandler dataHandler = new DataHandlerCounterTest.TestDataHandler(handlerSleepTime |
| 17 | + ); |
| 18 | + DataHandlerCounterTest.TestDataHandlerCounter counter = new DataHandlerCounterTest.TestDataHandlerCounter( |
| 19 | + dataHandler); |
| 20 | + // check the initial process count |
| 21 | + NUnit.Framework.Assert.AreEqual(0, dataHandler.GetProcessCount()); |
| 22 | + long count = dataHandler.GetProcessCount(); |
| 23 | + Thread.Sleep(betweenChecksSleepTime); |
| 24 | + // check that process count has been updated |
| 25 | + NUnit.Framework.Assert.AreNotEqual(count, dataHandler.GetProcessCount()); |
| 26 | + counter.Close(); |
| 27 | + // ensure that last process on disable would be finished |
| 28 | + Thread.Sleep(betweenChecksSleepTime); |
| 29 | + long totalCount = dataHandler.GetProcessCount(); |
| 30 | + Thread.Sleep(betweenChecksSleepTime); |
| 31 | + // ensure that after disabling there are no new processes has been invoked |
| 32 | + NUnit.Framework.Assert.AreEqual(totalCount, dataHandler.GetProcessCount()); |
| 33 | + } |
| 34 | + |
| 35 | + [NUnit.Framework.Test] |
| 36 | + public virtual void OnEventAfterDisableTest() { |
| 37 | + DataHandlerCounterTest.TestDataHandlerCounter counter = new DataHandlerCounterTest.TestDataHandlerCounter( |
| 38 | + new DataHandlerCounterTest.TestDataHandler(100)); |
| 39 | + DataHandlerCounterTest.TestEvent testEvent = new DataHandlerCounterTest.TestEvent("test"); |
| 40 | + NUnit.Framework.Assert.DoesNotThrow(() => counter.OnEvent(testEvent, null)); |
| 41 | + counter.Close(); |
| 42 | + NUnit.Framework.Assert.That(() => { |
| 43 | + counter.OnEvent(testEvent, null); |
| 44 | + } |
| 45 | + , NUnit.Framework.Throws.InstanceOf<InvalidOperationException>().With.Message.EqualTo(PdfException.DataHandlerCounterHasBeenDisabled)) |
| 46 | +; |
| 47 | + } |
| 48 | + |
| 49 | + [NUnit.Framework.Test] |
| 50 | + public virtual void MultipleRegisterHooksTest() { |
| 51 | + DataHandlerCounterTest.TestDataHandler dataHandler = new DataHandlerCounterTest.TestDataHandler(200); |
| 52 | + DataHandlerCounterTest.TestDataHandlerCounter counter = new DataHandlerCounterTest.TestDataHandlerCounter( |
| 53 | + dataHandler); |
| 54 | + DataHandlerCounterTest.TestDataHandlerCounter secondCounter = new DataHandlerCounterTest.TestDataHandlerCounter |
| 55 | + (dataHandler); |
| 56 | + NUnit.Framework.Assert.DoesNotThrow(() => counter.Close()); |
| 57 | + NUnit.Framework.Assert.DoesNotThrow(() => secondCounter.Close()); |
| 58 | + } |
| 59 | + |
| 60 | + [NUnit.Framework.Test] |
| 61 | + // count set explicitly as it is required that the test should log this message only once |
| 62 | + [LogMessage(iText.IO.LogMessageConstant.UNEXPECTED_EVENT_HANDLER_SERVICE_THREAD_EXCEPTION, Count = 1, LogLevel |
| 63 | + = LogLevelConstants.ERROR)] |
| 64 | + public virtual void TimedProcessWithExceptionTest() { |
| 65 | + int betweenChecksSleepTime = 500; |
| 66 | + int handlerSleepTime = 100; |
| 67 | + DataHandlerCounterTest.TestDataHandlerWithException dataHandler = new DataHandlerCounterTest.TestDataHandlerWithException |
| 68 | + (handlerSleepTime); |
| 69 | + DataHandlerCounterTest.TestDataHandlerCounter counter = new DataHandlerCounterTest.TestDataHandlerCounter( |
| 70 | + dataHandler); |
| 71 | + // check the initial process count |
| 72 | + NUnit.Framework.Assert.AreEqual(0, dataHandler.GetProcessCount()); |
| 73 | + Thread.Sleep(betweenChecksSleepTime); |
| 74 | + // check that process count has not been updated |
| 75 | + NUnit.Framework.Assert.AreEqual(0, dataHandler.GetProcessCount()); |
| 76 | + NUnit.Framework.Assert.DoesNotThrow(() => counter.Close()); |
| 77 | + } |
| 78 | + |
| 79 | + private class TestDataHandlerCounter : DataHandlerCounter<String, DataHandlerCounterTest.SimpleData> { |
| 80 | + public TestDataHandlerCounter(DataHandlerCounterTest.TestDataHandler dataHandler) |
| 81 | + : base(dataHandler) { |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private class SimpleData : EventData<String> { |
| 86 | + public SimpleData(String signature) |
| 87 | + : base(signature) { |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private class SimpleDataFactory : IEventDataFactory<String, DataHandlerCounterTest.SimpleData> { |
| 92 | + public virtual DataHandlerCounterTest.SimpleData Create(IEvent @event, IMetaInfo metaInfo) { |
| 93 | + return new DataHandlerCounterTest.SimpleData(@event.GetEventType()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private class TestDataHandler : EventDataHandler<String, DataHandlerCounterTest.SimpleData> { |
| 98 | + private readonly AtomicLong processCount = new AtomicLong(0); |
| 99 | + |
| 100 | + public TestDataHandler(long sleepTime) |
| 101 | + : base(new EventDataCacheComparatorBased<String, DataHandlerCounterTest.SimpleData>(new EventDataHandlerUtil.BiggerCountComparator |
| 102 | + <String, DataHandlerCounterTest.SimpleData>()), new DataHandlerCounterTest.SimpleDataFactory(), sleepTime |
| 103 | + , sleepTime) { |
| 104 | + } |
| 105 | + |
| 106 | + public override void TryProcessNext() { |
| 107 | + processCount.IncrementAndGet(); |
| 108 | + base.TryProcessNext(); |
| 109 | + } |
| 110 | + |
| 111 | + public override void TryProcessRest() { |
| 112 | + processCount.IncrementAndGet(); |
| 113 | + base.TryProcessRest(); |
| 114 | + } |
| 115 | + |
| 116 | + protected internal override bool Process(DataHandlerCounterTest.SimpleData data) { |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + public virtual long GetProcessCount() { |
| 121 | + return processCount.Get(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private class TestDataHandlerWithException : DataHandlerCounterTest.TestDataHandler { |
| 126 | + public TestDataHandlerWithException(long sleepTime) |
| 127 | + : base(sleepTime) { |
| 128 | + } |
| 129 | + |
| 130 | + public override void TryProcessNextAsync(bool? daemon) { |
| 131 | + throw new PdfException("Some exception message"); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private class TestEvent : IEvent { |
| 136 | + private readonly String type; |
| 137 | + |
| 138 | + public TestEvent(String type) { |
| 139 | + this.type = type; |
| 140 | + } |
| 141 | + |
| 142 | + public virtual String GetEventType() { |
| 143 | + return type; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments