|
1 |
| -using NUnit.Framework; |
| 1 | +using Lucene.Net.Attributes; |
| 2 | +using NUnit.Framework; |
2 | 3 | using RandomizedTesting.Generators;
|
| 4 | +using System; |
3 | 5 | using System.Collections.Generic;
|
4 | 6 | using JCG = J2N.Collections.Generic;
|
5 | 7 | using Assert = Lucene.Net.TestFramework.Assert;
|
@@ -68,5 +70,54 @@ public virtual void TestReadAndWrite()
|
68 | 70 | }
|
69 | 71 | }
|
70 | 72 | }
|
| 73 | + |
| 74 | + [Test] |
| 75 | + [LuceneNetSpecific] // LUCENENET issue #1003 |
| 76 | + public void TestTooManyAllocs() |
| 77 | + { |
| 78 | + // Use a mock allocator that doesn't waste memory |
| 79 | + ByteBlockPool pool = new ByteBlockPool(new MockAllocator(0)); |
| 80 | + pool.NextBuffer(); |
| 81 | + |
| 82 | + bool throwsException = false; |
| 83 | + int maxIterations = int.MaxValue / ByteBlockPool.BYTE_BLOCK_SIZE + 1; |
| 84 | + |
| 85 | + for (int i = 0; i < maxIterations; i++) |
| 86 | + { |
| 87 | + try |
| 88 | + { |
| 89 | + pool.NextBuffer(); |
| 90 | + } |
| 91 | + catch (OverflowException) |
| 92 | + { |
| 93 | + // The offset overflows on the last attempt to call NextBuffer() |
| 94 | + throwsException = true; |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + Assert.IsTrue(throwsException); |
| 100 | + Assert.IsTrue(pool.ByteOffset + ByteBlockPool.BYTE_BLOCK_SIZE < pool.ByteOffset); |
| 101 | + } |
| 102 | + |
| 103 | + private class MockAllocator : ByteBlockPool.Allocator |
| 104 | + { |
| 105 | + private readonly byte[] buffer; |
| 106 | + |
| 107 | + public MockAllocator(int blockSize) : base(blockSize) |
| 108 | + { |
| 109 | + buffer = Array.Empty<byte>(); |
| 110 | + } |
| 111 | + |
| 112 | + public override void RecycleByteBlocks(byte[][] blocks, int start, int end) |
| 113 | + { |
| 114 | + // No-op |
| 115 | + } |
| 116 | + |
| 117 | + public override byte[] GetByteBlock() |
| 118 | + { |
| 119 | + return buffer; |
| 120 | + } |
| 121 | + } |
71 | 122 | }
|
72 | 123 | }
|
0 commit comments