Lines 241-246
Link Here
|
241 |
} |
241 |
} |
242 |
} |
242 |
} |
243 |
|
243 |
|
|
|
244 |
|
245 |
/** Test whether the read is forbiden while somebody is writing |
246 |
*/ |
247 |
public void testWriteReadExclusion () throws Exception { |
248 |
assertTrue ("Succeeds to read the content", doWriteReadAtOnce (false)); |
249 |
} |
250 |
|
251 |
public void testWriteReadExclusionDeadlock () throws Exception { |
252 |
assertFalse ("Receives an exception", doWriteReadAtOnce (true)); |
253 |
} |
254 |
|
255 |
/** Starts two threads one tries to write and blocks for a while |
256 |
* the other tries to read meanwhile. If deadlockInWrite than the write |
257 |
* is stopped indefinitelly. |
258 |
* @return false if we got an InterruptedIOException or true is we |
259 |
* succeeded to read fully |
260 |
*/ |
261 |
private boolean doWriteReadAtOnce (final boolean deadlockInWrite) throws Exception { |
262 |
checkSetUp(); |
263 |
FileObject fold = getTestFolder1(root); |
264 |
final FileObject fo1 = getTestFile1(fold); |
265 |
|
266 |
String testStr = "text"; |
267 |
|
268 |
try { |
269 |
writeStr(fo1, testStr); |
270 |
} catch (IOException iex) { |
271 |
fsAssert( |
272 |
"expected move will success on writable FS", |
273 |
fs.isReadOnly() || fo1.isReadOnly() |
274 |
); |
275 |
// to make the conditions pass |
276 |
return !deadlockInWrite; |
277 |
} |
278 |
|
279 |
|
280 |
class DoWorkInOtherThread implements Runnable { |
281 |
Exception ex; |
282 |
|
283 |
public void run () { |
284 |
try { |
285 |
FileLock lock = fo1.lock(); |
286 |
|
287 |
OutputStream os = fo1.getOutputStream (lock); |
288 |
os.write (1); |
289 |
os.write (2); |
290 |
os.flush (); |
291 |
|
292 |
synchronized (this) { |
293 |
notify (); |
294 |
// block for a while so the other thread |
295 |
// can do the partial read |
296 |
wait (deadlockInWrite ? 0 : 1000); |
297 |
} |
298 |
|
299 |
os.write (3); |
300 |
os.write (4); |
301 |
os.close (); |
302 |
|
303 |
lock.releaseLock(); |
304 |
} catch (Exception ex) { |
305 |
this.ex = ex; |
306 |
} |
307 |
} |
308 |
|
309 |
public boolean executeDirectly () throws Exception { |
310 |
RequestProcessor.Task task; |
311 |
synchronized (this) { |
312 |
task = new RequestProcessor ("Writes with delay").post (this); |
313 |
wait (); |
314 |
} |
315 |
|
316 |
try { |
317 |
InputStream is = fo1.getInputStream (); |
318 |
byte[] arr = new byte[200]; |
319 |
int len = is.read (arr); |
320 |
assertEquals ("Read all four bytes", 4, len); |
321 |
for (int i = 1; i <= 4; i++) { |
322 |
assertEquals (i + " th byte is " + i, i, arr[i - 1]); |
323 |
} |
324 |
return true; |
325 |
} catch (InterruptedIOException ex) { |
326 |
// InterruptedIOException is fine, means we could not read the stream |
327 |
return false; |
328 |
} finally { |
329 |
synchronized (this) { |
330 |
// let the writer thread finish |
331 |
notifyAll (); |
332 |
} |
333 |
task.waitFinished (); |
334 |
} |
335 |
} |
336 |
} |
337 |
|
338 |
|
339 |
DoWorkInOtherThread d = new DoWorkInOtherThread (); |
340 |
return d.executeDirectly (); |
341 |
} |
342 |
|
244 |
public void testGetPath1() { |
343 |
public void testGetPath1() { |
245 |
checkSetUp(); |
344 |
checkSetUp(); |
246 |
FileObject fold1 = getTestFolder1(root); |
345 |
FileObject fold1 = getTestFolder1(root); |