Skip to content

Commit 22eefb4

Browse files
committed
Add tests for PathReadingSubscription.
1 parent f06f216 commit 22eefb4

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed

src/main/java/io/github/nstdio/http/ext/PathReadingSubscription.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ public void request(long n) {
6262
var sub = subscriber;
6363
while (n-- > 0) {
6464
ByteBuffer buff = ByteBuffer.allocate(DEFAULT_BUFF_CAPACITY);
65-
int r = chan.read(buff);
66-
if (r != -1) {
65+
if (chan.read(buff) != -1) {
6766
buff.flip();
6867
sub.onNext(Collections.singletonList(buff));
6968
} else {
70-
completed.set(true);
69+
cancel();
7170
sub.onComplete();
7271
break;
7372
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright (C) 2023 Edgar Asatryan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.nstdio.http.ext
18+
19+
import org.junit.jupiter.api.Test
20+
import org.junit.jupiter.api.extension.ExtendWith
21+
import org.junit.jupiter.api.io.TempDir
22+
import org.mockito.Mock
23+
import org.mockito.Mockito.argThat
24+
import org.mockito.Mockito.mock
25+
import org.mockito.Mockito.verify
26+
import org.mockito.Mockito.verifyNoInteractions
27+
import org.mockito.Mockito.verifyNoMoreInteractions
28+
import org.mockito.junit.jupiter.MockitoExtension
29+
import java.io.IOException
30+
import java.nio.ByteBuffer
31+
import java.nio.file.Files
32+
import java.nio.file.Path
33+
import java.nio.file.StandardOpenOption.CREATE
34+
import java.util.concurrent.Flow.Subscriber
35+
36+
@ExtendWith(MockitoExtension::class)
37+
class PathReadingSubscriptionTest {
38+
@TempDir
39+
private lateinit var baseDir: Path
40+
41+
@Mock
42+
lateinit var mockSub: Subscriber<List<ByteBuffer>>
43+
44+
@Test
45+
fun `Should read content of file`() {
46+
//given
47+
val content = "abcdef"
48+
val file = baseDir.resolve("text")
49+
Files.write(file, content.toByteArray(), CREATE)
50+
51+
val sub = PathReadingSubscription(mockSub, SimpleStreamFactory(), file)
52+
53+
//when
54+
sub.request(1)
55+
sub.request(1)
56+
57+
//then
58+
verify(mockSub).onNext(listOf(content.toByteBuffer()))
59+
verify(mockSub).onComplete()
60+
verifyNoMoreInteractions(mockSub)
61+
}
62+
63+
@Test
64+
fun `Should report error if IO error occures`() {
65+
//given
66+
val sub = PathReadingSubscription(mockSub, SimpleStreamFactory(), baseDir.resolve("text"))
67+
68+
//when
69+
sub.request(1)
70+
71+
//then
72+
verify(mockSub).onError(argThat { it is IOException })
73+
}
74+
75+
@Test
76+
fun `Should not invoke sub when canceled`() {
77+
//given
78+
val sub = PathReadingSubscription(mockSub, SimpleStreamFactory(), baseDir.resolve("text"))
79+
80+
//when
81+
sub.cancel()
82+
sub.request(1)
83+
84+
//then
85+
verifyNoInteractions(mockSub)
86+
}
87+
88+
@Test
89+
fun `Should report error if negative requested`() {
90+
//given
91+
@Suppress("UNCHECKED_CAST")
92+
val mockSub = mock(Subscriber::class.java) as Subscriber<List<ByteBuffer>>
93+
val sub = PathReadingSubscription(mockSub, SimpleStreamFactory(), baseDir.resolve("text"))
94+
95+
//when
96+
sub.request(-1)
97+
98+
//then
99+
verify(mockSub).onError(argThat { it.message == "non-positive request" })
100+
}
101+
}

0 commit comments

Comments
 (0)