Skip to content

Commit

Permalink
jarek/6529: fix flaky test (#6536)
Browse files Browse the repository at this point in the history
* #6529: add volatile

* #6529: add synchronizedList

* #6529: add unmodifiableList

* #6529: add synchronizedList

* #6529: create copy of list

* #6529: clean

* #6529: make a copy in getters

* #6529: make clear method as synchronized
  • Loading branch information
jaroslawmalekcodete authored and scottdraves committed Dec 19, 2017
1 parent 7144103 commit fa7eb07
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
import java.util.ArrayList;
import java.util.List;

import static java.util.Arrays.asList;
import static java.util.Collections.synchronizedList;

public class KernelSocketsTest extends KernelSockets {

private List<Message> publishedMessages = new ArrayList<>();
private List<Message> sentMessages = new ArrayList<>();
private volatile List<Message> publishedMessages = synchronizedList(new ArrayList<>());
private volatile List<Message> sentMessages = synchronizedList(new ArrayList<>());

@Override
public void publish(Message message) {
Expand All @@ -37,15 +40,19 @@ public void send(Message message) {
}

public List<Message> getPublishedMessages() {
return new ArrayList<>(publishedMessages);
return copy(this.publishedMessages);
}

public List<Message> getSentMessages() {
return new ArrayList<>(sentMessages);
return copy(this.sentMessages);
}

private List<Message> copy(List<Message> list) {
return asList(list.toArray(new Message[0]));
}

public void clear() {
publishedMessages = new ArrayList<>();
sentMessages = new ArrayList<>();
public synchronized void clear() {
publishedMessages = synchronizedList(new ArrayList<>());
sentMessages = synchronizedList(new ArrayList<>());
}
}
21 changes: 12 additions & 9 deletions kernel/base/src/test/java/com/twosigma/beakerx/KernelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.twosigma.beakerx;

import static com.twosigma.beakerx.kernel.magic.command.ClasspathAddMvnDepsMagicCommandTest.TEST_MVN_CACHE;
import static java.util.Collections.unmodifiableList;

import com.twosigma.beakerx.autocomplete.AutocompleteResult;
import com.twosigma.beakerx.evaluator.Evaluator;
Expand Down Expand Up @@ -59,6 +60,7 @@
import com.twosigma.beakerx.kernel.msg.MessageCreator;
import com.twosigma.beakerx.kernel.threads.ExecutionResultSender;
import com.twosigma.beakerx.message.Message;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -68,6 +70,7 @@
import java.util.Map;
import java.util.Observer;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.assertj.core.util.Lists;

Expand Down Expand Up @@ -107,18 +110,18 @@ public KernelTest(String id, Evaluator evaluator) {
KernelManager.register(this);
}

private void initMavenResolverParam(){
private void initMavenResolverParam() {
this.mavenResolverParam = new MavenJarResolver.ResolverParams(
new File(TEST_MVN_CACHE).getAbsolutePath(),
getTempFolder().toString() + MavenJarResolver.MVN_DIR,
true);
new File(TEST_MVN_CACHE).getAbsolutePath(),
getTempFolder().toString() + MavenJarResolver.MVN_DIR,
true);
}

private void initMagicCommands() {
this.magicCommandTypes = new ArrayList<>();
this.magicCommandTypes.addAll(Lists.newArrayList(
new MagicCommandType(JavaScriptMagicCommand.JAVASCRIPT, "", new JavaScriptMagicCommand()),
new MagicCommandType(JSMagicCommand.JAVASCRIPT,"", new JSMagicCommand()),
new MagicCommandType(JSMagicCommand.JAVASCRIPT, "", new JSMagicCommand()),
new MagicCommandType(HtmlMagicCommand.HTML, "", new HtmlMagicCommand()),
new MagicCommandType(HtmlAliasMagicCommand.HTML, "", new HtmlAliasMagicCommand()),
new MagicCommandType(BashMagicCommand.BASH, "", new BashMagicCommand()),
Expand Down Expand Up @@ -268,19 +271,19 @@ public EvaluatorParameters getSetShellOptions() {
}

public List<Message> getPublishedMessages() {
return publishedMessages;
return unmodifiableList(publishedMessages);
}

public List<Message> getSentMessages() {
return sentMessages;
return unmodifiableList(sentMessages);
}

public void clearPublishedMessages() {
this.publishedMessages = new ArrayList<>();
this.publishedMessages = new ArrayList<>(new ArrayList<>());
}

public void clearSentMessages() {
this.sentMessages = new ArrayList<>();
this.sentMessages = new ArrayList<>(new ArrayList<>());
}

public void clearMessages() {
Expand Down

0 comments on commit fa7eb07

Please sign in to comment.