Commit 9f425343 authored by Andy Wilkinson's avatar Andy Wilkinson

Make FileSystemWatcherTests thread-safe

The list of changes is written to on one thread and read from on
another. Without some form of sychronization this is not thread-safe.

This commit makes changes a synchronized list which should guarantee
that the reading thread can see the changes made by the writing thread.
It also removes a redundant call to clear the list of changes at the
start of waitsForPollingInterval.

See gh-6038
parent a98d1a41
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -23,6 +23,7 @@ import java.io.FileOutputStream; ...@@ -23,6 +23,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
...@@ -56,7 +57,8 @@ public class FileSystemWatcherTests { ...@@ -56,7 +57,8 @@ public class FileSystemWatcherTests {
private FileSystemWatcher watcher; private FileSystemWatcher watcher;
private List<Set<ChangedFiles>> changes = new ArrayList<Set<ChangedFiles>>(); private List<Set<ChangedFiles>> changes = Collections
.synchronizedList(new ArrayList<Set<ChangedFiles>>());
@Rule @Rule
public TemporaryFolder temp = new TemporaryFolder(); public TemporaryFolder temp = new TemporaryFolder();
...@@ -158,7 +160,6 @@ public class FileSystemWatcherTests { ...@@ -158,7 +160,6 @@ public class FileSystemWatcherTests {
@Test @Test
public void waitsForPollingInterval() throws Exception { public void waitsForPollingInterval() throws Exception {
this.changes.clear();
setupWatcher(100, 1); setupWatcher(100, 1);
File folder = startWithNewFolder(); File folder = startWithNewFolder();
touch(new File(folder, "test1.txt")); touch(new File(folder, "test1.txt"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment