aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/DistributedAnswerTest.java
blob: 295d8e006966d7bcb2cd88fa67bc9bae5b6c8c1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.process.cluster.hz;

import com.hazelcast.cluster.Member;
import java.io.IOException;
import java.util.UUID;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.process.cluster.hz.HazelcastMember.Attribute.NODE_NAME;

public class DistributedAnswerTest {

  private final Member member = newMember(uuidFromInt(9));
  private final DistributedAnswer<String> underTest = new DistributedAnswer<>();

  @Test
  public void getMembers_return_all_members() {
    underTest.setAnswer(member, "foo");
    underTest.setTimedOut(newMember(uuidFromInt(0)));
    underTest.setFailed(newMember(uuidFromInt(1)), new IOException("BOOM"));

    assertThat(underTest.getMembers()).hasSize(3);
  }

  @Test
  public void test_call_with_unknown_member() {
    assertThat(underTest.getAnswer(member)).isEmpty();
    assertThat(underTest.hasTimedOut(member)).isFalse();
    assertThat(underTest.getFailed(member)).isEmpty();
  }

  @Test
  public void test_setAnswer() {
    underTest.setAnswer(member, "foo");

    assertThat(underTest.getAnswer(member)).hasValue("foo");
    assertThat(underTest.hasTimedOut(member)).isFalse();
    assertThat(underTest.getFailed(member)).isEmpty();
  }

  @Test
  public void test_setTimedOut() {
    underTest.setTimedOut(member);

    assertThat(underTest.getAnswer(member)).isEmpty();
    assertThat(underTest.hasTimedOut(member)).isTrue();
    assertThat(underTest.getFailed(member)).isEmpty();
  }

  @Test
  public void test_setFailed() {
    IOException e = new IOException();
    underTest.setFailed(member, e);

    assertThat(underTest.getAnswer(member)).isEmpty();
    assertThat(underTest.hasTimedOut(member)).isFalse();
    assertThat(underTest.getFailed(member)).hasValue(e);
  }

  @Test
  public void member_can_be_referenced_multiple_times() {
    underTest.setTimedOut(member);
    underTest.setAnswer(member, "foo");
    IOException exception = new IOException();
    underTest.setFailed(member, exception);

    assertThat(underTest.hasTimedOut(member)).isTrue();
    assertThat(underTest.getAnswer(member)).hasValue("foo");
    assertThat(underTest.getFailed(member)).hasValue(exception);
  }

  @Test
  public void propagateExceptions_does_nothing_if_no_members() {
    // no errors
    underTest.propagateExceptions();
  }

  @Test
  public void propagateExceptions_does_nothing_if_no_errors() {
    underTest.setAnswer(newMember(uuidFromInt(3)), "bar");

    // no errors
    underTest.propagateExceptions();
  }

  @Test
  public void propagateExceptions_throws_ISE_if_at_least_one_timeout() {
    UUID uuid = uuidFromInt(4);
    UUID otherUuid = uuidFromInt(5);

    underTest.setAnswer(newMember(uuid), "baz");
    underTest.setTimedOut(newMember(otherUuid));

    assertThatThrownBy(underTest::propagateExceptions)
      .isInstanceOf(IllegalStateException.class)
      .hasMessage("Distributed cluster action timed out in cluster nodes " + otherUuid);
  }

  @Test
  public void propagateExceptions_throws_ISE_if_at_least_one_failure() {
    UUID foo = uuidFromInt(0);
    UUID bar = uuidFromInt(1);

    underTest.setAnswer(newMember(bar), "baz");
    underTest.setFailed(newMember(foo), new IOException("BOOM"));

    assertThatThrownBy(underTest::propagateExceptions)
      .isInstanceOf(IllegalStateException.class)
      .hasMessage("Distributed cluster action in cluster nodes " + foo + " (other nodes may have timed out)");
  }

  @Test
  public void getSingleAnswer_returns_first_answer() {
    underTest.setAnswer(newMember(uuidFromInt(0)), "foo");

    assertThat(underTest.getSingleAnswer()).isNotEmpty();
  }

  private UUID uuidFromInt(int digit) {
    return UUID.fromString("00000000-0000-0000-0000-00000000000" + digit);
  }

  @Test
  public void getSingleAnswer_whenNoAnswer_returnEmptyOptional() {
    assertThat(underTest.getSingleAnswer()).isEmpty();
  }

  private static Member newMember(UUID uuid) {
    Member member = mock(Member.class);
    when(member.getUuid()).thenReturn(uuid);
    when(member.getAttribute(NODE_NAME.getKey())).thenReturn(uuid.toString());
    return member;
  }
}