Skip to content

Commit b78dd8f

Browse files
committed
add tests on IOs
1 parent 25acef7 commit b78dd8f

File tree

4 files changed

+71
-22
lines changed

4 files changed

+71
-22
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/io/IOUtil.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,15 @@ public static AsyncWork<Integer,IOException> readFullyAsync(
140140
if (ondone != null) ondone.run(new Pair<>(read.getResult(), null));
141141
return read;
142142
}
143-
if (read.getResult().intValue() <= 0) {
144-
if (ondone != null) ondone.run(new Pair<>(Integer.valueOf(done), null));
145-
return new AsyncWork<>(Integer.valueOf(done), null);
146-
}
147-
if (ondone != null) ondone.run(new Pair<>(Integer.valueOf(read.getResult().intValue() + done),null));
148-
return new AsyncWork<>(Integer.valueOf(read.getResult().intValue() + done),null);
143+
if (read.getResult().intValue() <= 0) return success(Integer.valueOf(done), ondone);
144+
return success(Integer.valueOf(read.getResult().intValue() + done), ondone);
149145
}
150146
if (read.getResult().intValue() <= 0) {
151147
if (done == 0) {
152148
if (ondone != null) ondone.run(new Pair<>(read.getResult(), null));
153149
return read;
154150
}
155-
if (ondone != null) ondone.run(new Pair<>(Integer.valueOf(done), null));
156-
return new AsyncWork<>(Integer.valueOf(done), null);
151+
return success(Integer.valueOf(done), ondone);
157152
}
158153
return readFullyAsync(io, buffer, read.getResult().intValue() + done, ondone);
159154
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/test/io/TestReadable.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ public void run() {
146146
done.error(new Exception("Method readFullyAsync didn't call ondone before listeners"));
147147
return;
148148
}
149-
onDoneBefore.set(false);
150149
if (res.hasError()) {
151150
done.error(res.getError());
152151
return;
@@ -185,7 +184,13 @@ public void run() {
185184
}
186185
i.inc();
187186
buffer.clear();
188-
read.set(io.readFullyAsync(buffer, ondone));
187+
if ((i.get() % 7) == 0) {
188+
onDoneBefore.set(true);
189+
read.set(io.readFullyAsync(buffer, null));
190+
} else {
191+
onDoneBefore.set(false);
192+
read.set(io.readFullyAsync(buffer, ondone));
193+
}
189194
} while (read.get().isUnblocked());
190195
read.get().listenInline(this);
191196
}
@@ -388,7 +393,10 @@ public void run() {
388393
}
389394
pos.inc();
390395
// kind of random skip, but always the same to reproduce bug if any
391-
skip.set(io.skipAsync(toSkip.get(), ondone));
396+
if ((toSkip.get() % 7) == 0)
397+
skip.set(io.skipAsync(toSkip.get(), null));
398+
else
399+
skip.set(io.skipAsync(toSkip.get(), ondone));
392400
skip.get().listenInline(skipListener.get());
393401
}
394402
};
@@ -419,6 +427,8 @@ public void run() {
419427
pos.add(skipped);
420428
// kind of random skip, but always the same to reproduce bug if any
421429
toSkip.set(toSkip.get() + 1 + toSkip.get()/2);
430+
if ((toSkip.get() % 7) == 0)
431+
onDoneBefore.set(true);
422432
if (pos.get() >= size) {
423433
result.unblock();
424434
return;
@@ -434,6 +444,13 @@ public void run() {
434444

435445
result.blockThrow(0);
436446

447+
long skipped = io.skipAsync(-(nbBuf + 2) * testBuf.length).blockResult(0).longValue();
448+
if (io instanceof IO.Readable.Seekable) {
449+
Assert.assertEquals(-pos.get(), skipped);
450+
} else {
451+
Assert.assertEquals(0, skipped);
452+
}
453+
437454
io.close();
438455
}
439456

@@ -454,6 +471,14 @@ public void testSkipSyncNegativeValue() throws Exception {
454471
if (skipped != 0)
455472
throw new Exception("Readable is not supposed to be able to skip with a negative value, skipping -10 bytes returned " + skipped + " while 0 was expected.");
456473
}
474+
skipped = io.skipSync(-2 * testBuf.length);
475+
if (io instanceof IO.Readable.Seekable) {
476+
if (skipped != testBuf.length + testBuf.length / 2)
477+
throw new Exception("Skip beyond beginning of IO on Readable.Seekable is supposed to go to the offset 0. Skipping " + (-2 * testBuf.length) + " returned " + skipped + " but expected was " + (testBuf.length + testBuf.length / 2));
478+
} else {
479+
if (skipped != 0)
480+
throw new Exception("Readable is not supposed to be able to skip with a negative value, skipping -10 bytes returned " + skipped + " while 0 was expected.");
481+
}
457482
io.readSync(buffer);
458483
Assert.assertEquals("Invalid byte read after skipSync with negative value", testBuf[testBuf.length / 2 + (int)skipped], b[0]);
459484
io.close();

net.lecousin.core/src/test/java/net/lecousin/framework/core/test/io/TestReadableSeekable.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ public void run() {
114114
sp.error(new Exception("Method readAsync didn't call ondone before listeners"));
115115
return;
116116
}
117-
onDoneBefore.set(false);
118117

119118
if (offsets.isEmpty() && j.get() == testBuf.length) {
120119
if (read.get().getResult().intValue() > 0) {
@@ -156,7 +155,13 @@ public void run() {
156155
}
157156

158157
buffer.clear();
159-
read.set(io.readAsync(offset.get()*testBuf.length+j.get(), buffer, ondone));
158+
if ((j.get() % 7) == 0) {
159+
onDoneBefore.set(true);
160+
read.set(io.readAsync(offset.get()*testBuf.length+j.get(), buffer, null));
161+
} else {
162+
onDoneBefore.set(false);
163+
read.set(io.readAsync(offset.get()*testBuf.length+j.get(), buffer, ondone));
164+
}
160165
} while (read.get().isUnblocked());
161166
read.get().listenInline(this);
162167
}
@@ -239,7 +244,6 @@ public void run() {
239244
sp.error(new Exception("Method readFullyAsync didn't call ondone before listeners"));
240245
return;
241246
}
242-
onDoneBefore.set(false);
243247

244248
if (offset.get() == nbBuf) {
245249
if (read.get().getResult().intValue() > 0) {
@@ -272,7 +276,13 @@ public void run() {
272276
}
273277

274278
buffer.clear();
275-
read.set(io.readFullyAsync(offset.get()*testBuf.length, buffer, ondone));
279+
if ((offset.get() % 7) == 0) {
280+
onDoneBefore.set(true);
281+
read.set(io.readFullyAsync(offset.get()*testBuf.length, buffer, null));
282+
} else {
283+
onDoneBefore.set(false);
284+
read.set(io.readFullyAsync(offset.get()*testBuf.length, buffer, ondone));
285+
}
276286
} while (read.get().isUnblocked());
277287
read.get().listenInline(this);
278288
}
@@ -402,12 +412,18 @@ private ISynchronizationPoint<?> testSeekAsync(ISynchronizationPoint<?> startOn,
402412
SynchronizationPoint<Exception> sp = new SynchronizationPoint<>();
403413

404414
MutableBoolean onDoneBefore = new MutableBoolean(false);
405-
RunnableWithParameter<Pair<Long,IOException>> ondone = new RunnableWithParameter<Pair<Long,IOException>>() {
406-
@Override
407-
public void run(Pair<Long, IOException> param) {
408-
onDoneBefore.set(true);
409-
}
410-
};
415+
RunnableWithParameter<Pair<Long,IOException>> ondone;
416+
if ((expectedPosition % 3) == 0) {
417+
onDoneBefore.set(true);
418+
ondone = null;
419+
} else {
420+
ondone = new RunnableWithParameter<Pair<Long,IOException>>() {
421+
@Override
422+
public void run(Pair<Long, IOException> param) {
423+
onDoneBefore.set(true);
424+
}
425+
};
426+
}
411427

412428
startOn.listenInline(new Runnable() {
413429
@Override
@@ -428,7 +444,8 @@ public void run() {
428444
sp.error(new Exception("Method seekAsync didn't call ondone before listeners"));
429445
return;
430446
}
431-
onDoneBefore.set(false);
447+
if (ondone != null)
448+
onDoneBefore.set(false);
432449
long p = seek.getResult().longValue();
433450
if (p != expectedPosition) {
434451
sp.error(new Exception("Invalid seek: returned position " + p + ", expected is " + expectedPosition + " after seek(" + type + ", " + move + ")"));

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/io/TestIOUtil.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,16 @@ public void testCopy() throws Exception {
153153
tmp1.delete();
154154
tmp2.delete();
155155
}
156+
157+
@Test(timeout=120000)
158+
public void testToTempFile() throws Exception {
159+
byte[] b = new byte[65000];
160+
for (int i = 0; i < b.length; ++i)
161+
b[i] = (byte)(i % 167);
162+
File f = IOUtil.toTempFile(b).blockResult(0);
163+
b = IOUtil.readFully(f, Task.PRIORITY_NORMAL).blockResult(0);
164+
for (int i = 0; i < b.length; ++i)
165+
Assert.assertEquals((byte)(i % 167), b[i]);
166+
f.delete();
167+
}
156168
}

0 commit comments

Comments
 (0)