22
33import org .apache .spark .SparkConf ;
44import org .apache .spark .SparkEnv ;
5- import org .junit .After ;
6- import org .junit .Before ;
7- import org .junit .Test ;
5+ import org .junit .*;
86
97import java .io .*;
108import java .nio .ByteBuffer ;
1311import java .util .Arrays ;
1412import java .util .List ;
1513import java .util .Random ;
14+ import java .util .concurrent .ExecutorService ;
15+ import java .util .concurrent .Executors ;
1616import java .util .concurrent .TimeUnit ;
1717import java .util .regex .Pattern ;
1818import java .util .stream .Collectors ;
1919import java .util .stream .Stream ;
2020
2121import static org .junit .Assume .*;
2222import static org .junit .Assert .*;
23- import static org .mockito .Mockito .mock ;
24- import static org .mockito .Mockito .when ;
23+ import static org .mockito .Mockito .*;
2524
2625/**
2726 * Tests functionality of {@link PlasmaInputStream} and {@link PlasmaOutputStream}
@@ -30,15 +29,15 @@ public class PlasmaOutputInputStreamSuite {
3029
3130 private final static int DEFAULT_BUFFER_SIZE = 4096 ;
3231
33- private final String plasmaStoreServer = "plasma-store-server" ;
34- private final String plasmaStoreSocket = "/tmp/PlasmaOutputInputStreamSuite_socket_file" ;
35- private final long memoryInBytes = 1000000000 ;
32+ private final static String plasmaStoreServer = "plasma-store-server" ;
33+ private final static String plasmaStoreSocket = "/tmp/PlasmaOutputInputStreamSuite_socket_file" ;
34+ private final static long memoryInBytes = 1000000000 ;
3635
37- private Process process ;
36+ private static Process process ;
3837 private final Random random = new Random ();
3938
40- @ Before
41- public void setUp () {
39+ @ BeforeClass
40+ public static void setUp () {
4241 boolean isAvailable = isPlasmaJavaAvailable ();
4342 assumeTrue ("Please make sure libplasma_java.so is installed" +
4443 " under LD_LIBRARY_PATH or java.library.path" , isAvailable );
@@ -63,24 +62,6 @@ public void testWithNullData() throws IOException {
6362 pos .write (null );
6463 }
6564
66- @ Test
67- public void testSingleWriteRead () {
68- String blockId = "block_id_" + random .nextInt (10000000 );
69- byte [] bytesWrite = prepareByteBlockToWrite (1 );
70- PlasmaOutputStream pos = new PlasmaOutputStream (blockId );
71- for (byte b : bytesWrite ) {
72- pos .write (b );
73- }
74-
75- byte [] bytesRead = new byte [bytesWrite .length ];
76- PlasmaInputStream pis = new PlasmaInputStream (blockId );
77- for (int i = 0 ; i < bytesRead .length ; i ++) {
78- bytesRead [i ] = (byte ) pis .read ();
79- }
80-
81- assertArrayEquals (bytesWrite , bytesRead );
82- }
83-
8465 @ Test
8566 public void testBufferWriteRead () throws IOException {
8667 String blockId = "block_id_" + random .nextInt (10000000 );
@@ -92,6 +73,9 @@ public void testBufferWriteRead() throws IOException {
9273 PlasmaInputStream pis = new PlasmaInputStream (blockId );
9374 pis .read (bytesRead );
9475 assertArrayEquals (bytesWrite , bytesRead );
76+
77+ PlasmaUtils .remove (blockId );
78+ assertFalse (PlasmaUtils .contains (blockId ));
9579 }
9680
9781 @ Test
@@ -108,8 +92,10 @@ public void testPartialBlockWriteRead() throws IOException {
10892 while ((len = pis .read (buffer )) != -1 ) {
10993 bytesRead .put (buffer , 0 , len );
11094 }
111-
11295 assertArrayEquals (bytesWrite , bytesRead .array ());
96+
97+ PlasmaUtils .remove (blockId );
98+ assertFalse (PlasmaUtils .contains (blockId ));
11399 }
114100
115101 @ Test
@@ -125,12 +111,46 @@ public void testMultiBlocksWriteRead() throws IOException {
125111 while (pis .read (buffer ) != -1 ) {
126112 bytesRead .put (buffer );
127113 }
128-
129114 assertArrayEquals (bytesWrite , bytesRead .array ());
115+
116+ PlasmaUtils .remove (blockId );
117+ assertFalse (PlasmaUtils .contains (blockId ));
118+ }
119+
120+ @ Test
121+ public void testMultiThreadReadWrite () throws InterruptedException {
122+ int processNum = Runtime .getRuntime ().availableProcessors ();
123+ ExecutorService threadPool = Executors .newFixedThreadPool (processNum );
124+ for (int i = 0 ; i < 10 * processNum ; i ++) {
125+ threadPool .submit (() -> {
126+ try {
127+ String blockId = "block_id_" + random .nextInt (10000000 );
128+ byte [] bytesWrite = prepareByteBlockToWrite (5.7 );
129+ PlasmaOutputStream pos = new PlasmaOutputStream (blockId );
130+ pos .write (bytesWrite );
131+
132+ ByteBuffer bytesRead = ByteBuffer .allocate (bytesWrite .length );
133+ PlasmaInputStream pis = new PlasmaInputStream (blockId );
134+ byte [] buffer = new byte [DEFAULT_BUFFER_SIZE ];
135+ int len ;
136+ while ((len = pis .read (buffer )) != -1 ) {
137+ bytesRead .put (buffer , 0 , len );
138+ }
139+ assertArrayEquals (bytesWrite , bytesRead .array ());
140+
141+ PlasmaUtils .remove (blockId );
142+ assertFalse (PlasmaUtils .contains (blockId ));
143+ } catch (IOException ex ) {
144+ ex .printStackTrace ();
145+ }
146+ });
147+ }
148+ threadPool .shutdown ();
149+ threadPool .awaitTermination (Long .MAX_VALUE , TimeUnit .MINUTES );
130150 }
131151
132- @ After
133- public void tearDown () {
152+ @ AfterClass
153+ public static void tearDown () {
134154 try {
135155 MyPlasmaClientHolder .close ();
136156 stopPlasmaStore ();
@@ -140,7 +160,7 @@ public void tearDown() {
140160 }
141161 }
142162
143- public boolean isPlasmaJavaAvailable () {
163+ public static boolean isPlasmaJavaAvailable () {
144164 boolean available = true ;
145165 try {
146166 System .loadLibrary ("plasma_java" );
@@ -150,22 +170,22 @@ public boolean isPlasmaJavaAvailable() {
150170 return available ;
151171 }
152172
153- private boolean isPlasmaStoreExist () {
173+ private static boolean isPlasmaStoreExist () {
154174 return Stream .of (System .getenv ("PATH" ).split (Pattern .quote (File .pathSeparator )))
155175 .map (Paths ::get )
156176 .anyMatch (path -> Files .exists (path .resolve (plasmaStoreServer )));
157177 }
158178
159- private Process startProcess (String command ) throws IOException {
179+ private static Process startProcess (String command ) throws IOException {
160180 List <String > cmdList = Arrays .stream (command .split (" " )).collect (Collectors .toList ());
161181 ProcessBuilder processBuilder = new ProcessBuilder (cmdList ).inheritIO ();
162182 Process process = processBuilder .start ();
163183 return process ;
164184 }
165185
166- private boolean startPlasmaStore () throws IOException , InterruptedException {
186+ private static boolean startPlasmaStore () throws IOException , InterruptedException {
167187 String command = plasmaStoreServer + " -s " + plasmaStoreSocket + " -m " + memoryInBytes ;
168- this . process = startProcess (command );
188+ process = startProcess (command );
169189 int ticktock = 60 ;
170190 if (process != null ) {
171191 while (!process .isAlive ()) {
@@ -180,7 +200,7 @@ private boolean startPlasmaStore() throws IOException, InterruptedException {
180200 return false ;
181201 }
182202
183- private void stopPlasmaStore () throws InterruptedException {
203+ private static void stopPlasmaStore () throws InterruptedException {
184204 if (process != null && process .isAlive ()) {
185205 process .destroyForcibly ();
186206 int ticktock = 60 ;
@@ -194,7 +214,7 @@ private void stopPlasmaStore() throws InterruptedException {
194214 }
195215 }
196216
197- private void deletePlasmaSocketFile () {
217+ private static void deletePlasmaSocketFile () {
198218 File socketFile = new File (plasmaStoreSocket );
199219 if (socketFile != null && socketFile .exists ()) {
200220 socketFile .delete ();
@@ -207,7 +227,7 @@ private byte[] prepareByteBlockToWrite(double numOfBlock) {
207227 return bytesToWrite ;
208228 }
209229
210- private void mockSparkEnv () {
230+ private static void mockSparkEnv () {
211231 SparkConf conf = new SparkConf ();
212232 conf .set ("spark.io.plasma.server.socket" , plasmaStoreSocket );
213233 SparkEnv mockEnv = mock (SparkEnv .class );
0 commit comments