Skip to content

Commit

Permalink
fix(tests): Flaky tests
Browse files Browse the repository at this point in the history
Remove sleep from tests and replace it with latch. Also in order to rad initialize a repo use addRemote method instead of pushing remotes to the list.

Signed-off-by: stelios maurommatakis <steliosmavr@cytech.gr>
  • Loading branch information
Stelios123 committed Apr 15, 2024
1 parent 3e7bea4 commit a888a47
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ private JComponent infoComponent() {
var gitRepoManager = GitRepositoryManager.getInstance(project);
var repos = gitRepoManager.getRepositories();
var myRepos = RadAction.getInitializedReposWithNodeConfigured(repos, false);
latch.countDown();
ApplicationManager.getApplication().invokeLater(() -> {
for (var repo : myRepos) {
projectSelect.addItem(repo);
}
if (myRepos.size() > 1) {
projectSelect.setVisible(true);
}
latch.countDown();
}, ModalityState.any());
});

Expand Down Expand Up @@ -295,6 +295,7 @@ public CompletableFuture<List<IssuePanel.AssigneesSelect.Assignee>> showEditPopu
jbPopup = popUpBuilder.createPopup(this.getData(), new IssuePanel.AssigneesSelect.AssigneeRender(), false, addField, res);
jbPopup.showUnderneathOf(parent);
listener = popUpBuilder.getListener();
latch = popUpBuilder.getLatch();
return res.thenApply(data -> {
if (Strings.isNullOrEmpty(addField.getText())) {
return data;
Expand Down Expand Up @@ -372,6 +373,7 @@ public CompletableFuture<List<IssuePanel.LabelSelect.Label>> showEditPopup(JComp
jbPopup = popUpBuilder.createPopup(this.getData(), new IssuePanel.LabelSelect.LabelRender(), false, addField, res);
jbPopup.showUnderneathOf(parent);
listener = popUpBuilder.getListener();
latch = popUpBuilder.getLatch();
return res.thenApply(data -> {
if (Strings.isNullOrEmpty(addField.getText())) {
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public CompletableFuture<List<Label>> showEditPopup(JComponent parent) {
jbPopup = popUpBuilder.createPopup(this.getData(), new LabelRender(), false, addField, res);
jbPopup.showUnderneathOf(parent);
listener = popUpBuilder.getListener();
latch = popUpBuilder.getLatch();
return res.thenApply(data -> {
if (Strings.isNullOrEmpty(addField.getText())) {
return data;
Expand Down Expand Up @@ -360,6 +361,7 @@ public CompletableFuture<List<Assignee>> showEditPopup(JComponent parent) {
jbPopup = popUpBuilder.createPopup(this.getData(), new IssuePanel.AssigneesSelect.AssigneeRender(), false, addField, res);
jbPopup.showUnderneathOf(parent);
listener = popUpBuilder.getListener();
latch = popUpBuilder.getLatch();
return res.thenApply(data -> {
if (Strings.isNullOrEmpty(addField.getText())) {
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ public CompletableFuture<List<LabelSelect.Label>> showEditPopup(JComponent paren
jbPopup = popUpBuilder.createPopup(this.getData(), getRender(), this.isSingleSelection(), addField, res);
jbPopup.showUnderneathOf(parent);
listener = popUpBuilder.getListener();
latch = popUpBuilder.getLatch();
return res.thenApply(data -> {
if (Strings.isNullOrEmpty(addField.getText())) {
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

import static network.radicle.jetbrains.radiclejetbrainsplugin.toolwindow.Utils.getHorizontalPanel;
Expand All @@ -42,6 +43,7 @@ public abstract class EmojiPanel<T> {
private JBPopup reactorsPopUp;
private JBPopup emojisPopUp;
private JBPopupListener popupListener;
private CountDownLatch latch;

protected EmojiPanel(SingleValueModel<T> model, List<Reaction> reactions, String discussionId,

Check warning on line 48 in src/main/java/network/radicle/jetbrains/radiclejetbrainsplugin/toolwindow/EmojiPanel.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'com.intellij.collaboration.ui.SingleValueModel' is declared in unstable package 'com.intellij.collaboration.ui' marked with @ApiStatus.Experimental

Check warning on line 48 in src/main/java/network/radicle/jetbrains/radiclejetbrainsplugin/toolwindow/EmojiPanel.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'com.intellij.collaboration.ui.SingleValueModel' is declared in unstable package 'com.intellij.collaboration.ui' marked with @ApiStatus.Experimental
RadDetails radDetails) {
Expand Down Expand Up @@ -122,6 +124,7 @@ public void mouseClicked(MouseEvent e) {
var popupBuilder = new PopupBuilder(170, 40);
emojisPopUp = popupBuilder.createHorizontalPopup(getEmojis(), new EmojiRender(), result);
popupListener = popupBuilder.getListener();
latch = popupBuilder.getLatch();
emojisPopUp.showUnderneathOf(emojiButton);
result.thenComposeAsync(selectedEmoji -> {
ApplicationManager.getApplication().executeOnPooledThread(() -> {
Expand Down Expand Up @@ -206,6 +209,10 @@ public JBPopupListener getPopupListener() {
return popupListener;
}

public CountDownLatch getLatch() {
return latch;
}

public JBPopup getEmojisPopUp() {
return emojisPopUp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.awt.FlowLayout;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;

public abstract class LabeledListPanelHandle<T> {
private final InlineIconButton editButton;
Expand All @@ -29,6 +30,7 @@ public abstract class LabeledListPanelHandle<T> {
private JLabel selectedValuesLabel;
public JBPopup jbPopup;
public JBPopupListener listener;
public CountDownLatch latch;

public LabeledListPanelHandle() {
progressLabel = new JLabel(new AnimatedIcon.Default());
Expand Down Expand Up @@ -89,6 +91,7 @@ public CompletableFuture<List<T>> showEditPopup(JComponent parent) {
jbPopup = popUpBuilder.createPopup(this.getData(), getRender(), this.isSingleSelection(), null, result);
jbPopup.showUnderneathOf(parent);
listener = popUpBuilder.getListener();
latch = popUpBuilder.getLatch();
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
public class PopupBuilder {
private static final Logger logger = LoggerFactory.getLogger(Utils.class);
private JBPopupListener myListener;
private CountDownLatch latch;
private Integer width;
private Integer height;

Expand All @@ -63,6 +64,7 @@ public JBPopup createHorizontalPopup(JPanel component, boolean resizable, boolea

public <T> JBPopup createHorizontalPopup(CompletableFuture<List<SelectionListCellRenderer.SelectableWrapper<T>>> myList,
SelectionListCellRenderer<T> rendered, CompletableFuture<List<T>> result) {
latch = new CountDownLatch(1);
var listModel = new CollectionListModel<SelectionListCellRenderer.SelectableWrapper<T>>();
var list = new JBList<>(listModel);
list.setVisibleRowCount(7);
Expand Down Expand Up @@ -94,6 +96,7 @@ public void beforeShown(@NotNull LightweightWindowEvent event) {
} else {
event.asPopup().pack(true, true);
}
latch.countDown();
} catch (Exception e) {
logger.warn("Unable to load popup data", e);
} finally {
Expand All @@ -118,6 +121,7 @@ public void beforeShown(@NotNull LightweightWindowEvent event) {
public <T> JBPopup createPopup(CompletableFuture<List<SelectionListCellRenderer.SelectableWrapper<T>>> myList,
SelectionListCellRenderer<T> rendered, boolean singleSelection,
JBTextField inputField, CompletableFuture<List<T>> result) {
latch = new CountDownLatch(1);
var listModel = new CollectionListModel<SelectionListCellRenderer.SelectableWrapper<T>>();
var list = new JBList<>(listModel);
list.setVisibleRowCount(7);
Expand Down Expand Up @@ -164,12 +168,13 @@ public void beforeShown(@NotNull LightweightWindowEvent event) {
}
ApplicationManager.getApplication().executeOnPooledThread(() -> {
try {
var dataList = myList.get(5, TimeUnit.SECONDS);
var dataList = myList.get(10, TimeUnit.SECONDS);
listModel.replaceAll(dataList);
if (dataList.isEmpty()) {
popUp.setPreferredSize(new Dimension(100, 100));
}
event.asPopup().pack(true, true);
latch.countDown();
} catch (Exception e) {
logger.warn("Unable to load popup data", e);
} finally {
Expand Down Expand Up @@ -197,7 +202,7 @@ public void onClosed(@NotNull LightweightWindowEvent event) {
.createPopup();
}

public JBPopup createPopup(CompletableFuture<List<String>> list, CountDownLatch latch) {
public JBPopup createPopup(CompletableFuture<List<String>> list, CountDownLatch myLatch) {
return JBPopupFactory.getInstance().createPopupChooserBuilder(new ArrayList<String>())
.setResizable(true)
.setMovable(true)
Expand All @@ -219,7 +224,7 @@ public void beforeShown(@NotNull LightweightWindowEvent event) {
ApplicationManager.getApplication().executeOnPooledThread(() -> {
try {
//Wait for the data to be ready
var isFinished = latch.await(5, TimeUnit.SECONDS);
var isFinished = myLatch.await(5, TimeUnit.SECONDS);
if (!isFinished) {
return;
}
Expand All @@ -244,4 +249,8 @@ public void beforeShown(@NotNull LightweightWindowEvent event) {
public JBPopupListener getListener() {
return myListener;
}

public CountDownLatch getLatch() {
return latch;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.intellij.util.messages.MessageBusConnection;
import com.intellij.util.ui.UIUtil;
import git4idea.GitCommit;
import git4idea.commands.GitImpl;
import git4idea.config.GitConfigUtil;
import git4idea.history.GitHistoryUtils;
import git4idea.repo.GitRemote;
Expand Down Expand Up @@ -45,7 +46,9 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import static network.radicle.jetbrains.radiclejetbrainsplugin.GitTestUtil.addRadRemote;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -169,8 +172,19 @@ public static void assertCmd(GeneralCommandLine cmd) {
}

protected void removeRemoteRadUrl(GitRepository repo) {
var gitImpl = new GitImpl();
for (GitRemote remote : repo.getRemotes()) {
repo.getRemotes().remove(remote);
gitImpl.removeRemote(repo, remote);
}
var myLatch = new CountDownLatch(1);
ApplicationManager.getApplication().executeOnPooledThread(() -> {
repo.update();
myLatch.countDown();
});
try {
myLatch.await(5, TimeUnit.SECONDS);
} catch (Exception e) {
logger.warn("Unable to remove remotes");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
import com.intellij.openapi.vfs.VirtualFile;
import git4idea.GitUtil;
import git4idea.GitVcs;
import git4idea.repo.GitRemote;
import git4idea.commands.GitImpl;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryManager;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import static network.radicle.jetbrains.radiclejetbrainsplugin.GitExecutor.cd;
Expand Down Expand Up @@ -48,7 +48,18 @@ public static GitRepository createGitRepository(
}

public static void addRadRemote(Project project, GitRepository repo) {
repo.getRemotes().add(new GitRemote("rad", List.of("rad://abcdef"), List.of("rad://abcdef"), List.of("rad://abcdef"), List.of("rad://abcdef")));
try {
var gitImpl = new GitImpl();
gitImpl.addRemote(repo, "rad", "rad://abcdef");
var myLatch = new CountDownLatch(1);
ApplicationManager.getApplication().executeOnPooledThread(() -> {
repo.update();
myLatch.countDown();
});
myLatch.await(5, TimeUnit.SECONDS);
} catch (Exception e) {
logger.warn("Unable to add remote", e);
}
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public void addRemoveTags() throws InterruptedException {

popupListener.beforeShown(new LightweightWindowEvent(fakePopup));
//Wait to load tags
Thread.sleep(1000);
tagSelect.latch.await(5, TimeUnit.SECONDS);
assertThat(listmodel.getSize()).isEqualTo(2);

var firstTag = (SelectionListCellRenderer.SelectableWrapper<IssuePanel.LabelSelect.Label>) listmodel.getElementAt(0);
Expand All @@ -294,8 +294,8 @@ public void addRemoveTags() throws InterruptedException {
public void createNewIssueTest() throws InterruptedException {
var newIssuePanel = new CreateIssuePanel(issueTabController, myProject);
var tabs = newIssuePanel.create();
newIssuePanel.latch.await();
executeUiTasks();
newIssuePanel.latch.await();

var panel = (JPanel) tabs.getComponents()[tabs.getComponents().length - 1];
var children = panel.getComponents();
Expand Down Expand Up @@ -333,7 +333,8 @@ public void createNewIssueTest() throws InterruptedException {
popUpListener.beforeShown(new LightweightWindowEvent(fakePopup));

//Wait to load delegates
Thread.sleep(1000);
var res1 = assigneeSelect.latch.await(10, TimeUnit.SECONDS);
assertThat(res1).isEqualTo(true);
assertThat(listmodel.getSize()).isEqualTo(getTestProjects().get(0).delegates.size());

//Select the first did
Expand All @@ -358,7 +359,7 @@ public void createNewIssueTest() throws InterruptedException {
labelPopupListener.beforeShown(new LightweightWindowEvent(fakePopup));

//Wait to load labels
Thread.sleep(1000);
labelSelect.latch.await(10, TimeUnit.SECONDS);
assertThat(labelListModel.getSize()).isEqualTo(2);

//Remove the first label
Expand Down Expand Up @@ -413,7 +414,7 @@ public void changeStateTest() throws InterruptedException {
// Trigger beforeShown method
popupListener.beforeShown(new LightweightWindowEvent(JBPopupFactory.getInstance().createPopupChooserBuilder(new ArrayList<String>()).createPopup()));
//Wait to load state
Thread.sleep(1000);
stateSelect.latch.await(5, TimeUnit.SECONDS);
assertThat(listmodel.getSize()).isEqualTo(2);

var openState = (SelectionListCellRenderer.SelectableWrapper<IssuePanel.StateSelect.State>) listmodel.getElementAt(0);
Expand Down Expand Up @@ -484,7 +485,7 @@ public void addRemoveAssignersTest() throws InterruptedException {

popupListener.beforeShown(new LightweightWindowEvent(fakePopup));
//Wait to load delegates
Thread.sleep(500);
assigneesSelect.latch.await(5, TimeUnit.SECONDS);
assertThat(listmodel.getSize()).isEqualTo(3);

var firstAssignee = (SelectionListCellRenderer.SelectableWrapper<IssuePanel.AssigneesSelect.Assignee>) listmodel.getElementAt(0);
Expand All @@ -504,6 +505,7 @@ public void addRemoveAssignersTest() throws InterruptedException {
((SelectionListCellRenderer.SelectableWrapper<?>) listmodel.getElementAt(2)).selected = true;

popupListener.onClosed(new LightweightWindowEvent(assigneesSelect.jbPopup));
// Fix AlreadyDisposedException
Thread.sleep(1000);
executeUiTasks();
var res = response.poll(5, TimeUnit.SECONDS);
Expand Down Expand Up @@ -533,7 +535,7 @@ public void testReactions() throws InterruptedException {
popUpListener.beforeShown(new LightweightWindowEvent(JBPopupFactory.getInstance().createPopupChooserBuilder(new ArrayList<String>()).createPopup()));

//Wait for the emojis to load
Thread.sleep(1000);
emojiPanel.getLatch().await(5, TimeUnit.SECONDS);
var listmodel = jblist.getModel();
assertThat(listmodel.getSize()).isEqualTo(8);
//Select the first emoji
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,7 @@ public GitRemote findRadRemote(GitRepository repo, String myUrl) {
labelPopupListener.beforeShown(new LightweightWindowEvent(fakePopup));

//Wait to load labels
Thread.sleep(1000);
executeUiTasks();
labelSelect.latch.await(5, TimeUnit.SECONDS);
assertThat(labelListModel.getSize()).isEqualTo(2);

// Find create new patch button
Expand Down Expand Up @@ -595,8 +594,7 @@ public void addRemoveLabels() throws InterruptedException {

popupListener.beforeShown(new LightweightWindowEvent(fakePopup));
//Wait to load tags
executeUiTasks();
Thread.sleep(1000);
tagSelect.latch.await(5, TimeUnit.SECONDS);
assertThat(listmodel.getSize()).isEqualTo(2);

var firstTag = (SelectionListCellRenderer.SelectableWrapper<PatchProposalPanel.LabelSelect.Label>) listmodel.getElementAt(0);
Expand Down Expand Up @@ -637,8 +635,7 @@ public void testReactions() throws InterruptedException {
popUpListener.beforeShown(new LightweightWindowEvent(JBPopupFactory.getInstance().createPopupChooserBuilder(new ArrayList<String>()).createPopup()));

//Wait for the emojis to load
executeUiTasks();
Thread.sleep(1000);
emojiPanel.getLatch().await(5, TimeUnit.SECONDS);
var listmodel = jblist.getModel();
assertThat(listmodel.getSize()).isEqualTo(8);

Expand Down Expand Up @@ -716,8 +713,7 @@ public void changeStateTest() throws InterruptedException {
// Trigger beforeShown method
popupListener.beforeShown(new LightweightWindowEvent(JBPopupFactory.getInstance().createPopupChooserBuilder(new ArrayList<String>()).createPopup()));
//Wait to load state
executeUiTasks();
Thread.sleep(1000);
stateSelect.latch.await(5, TimeUnit.SECONDS);
assertThat(listmodel.getSize()).isEqualTo(3);

var openState = (SelectionListCellRenderer.SelectableWrapper<PatchProposalPanel.StateSelect.State>) listmodel.getElementAt(0);
Expand Down

0 comments on commit a888a47

Please sign in to comment.