Skip to content

Commit 1005801

Browse files
committed
[java][chat_rollout][03_method_call] Add ChatRolloutPercentageRepository
1 parent 1f23e37 commit 1005801

File tree

6 files changed

+36
-6
lines changed

6 files changed

+36
-6
lines changed

examples/java/java-chat_rollout-03_method_call/src/main/java/tv/codely/chat_rollout/ChatController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import java.util.Random;
44

55
public final class ChatController {
6-
private final int newChatRolloutPercentage;
6+
private final ChatRolloutPercentageRepository rolloutRepository;
77

8-
public ChatController(int newChatRolloutPercentage) {
9-
this.newChatRolloutPercentage = newChatRolloutPercentage;
8+
public ChatController(ChatRolloutPercentageRepository rolloutRepository) {
9+
this.rolloutRepository = rolloutRepository;
1010
}
1111

1212
public String pullMessages() {
13-
if (new Random().nextInt(100) <= newChatRolloutPercentage) {
13+
if (new Random().nextInt(100) <= rolloutRepository.percentage()) {
1414
return "Messages from the NEW chat system";
1515
} else {
1616
return "Messages from the OLD chat system";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package tv.codely.chat_rollout;
2+
3+
public interface ChatRolloutPercentageRepository {
4+
int percentage();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package tv.codely.chat_rollout;
2+
3+
public final class InMemoryChatRolloutPercentageRepository implements ChatRolloutPercentageRepository {
4+
@Override
5+
public int percentage() {
6+
return 30;
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package tv.codely.chat_rollout;
2+
3+
public final class AlwaysNewChatChatRolloutPercentageRepository implements ChatRolloutPercentageRepository {
4+
@Override
5+
public int percentage() {
6+
return 100;
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tv.codely.chat_rollout;
2+
3+
public final class AlwaysOldChatChatRolloutPercentageRepository implements ChatRolloutPercentageRepository {
4+
@Override
5+
public int percentage() {
6+
return 0;
7+
}
8+
}
9+

examples/java/java-chat_rollout-03_method_call/src/test/java/tv/codely/chat_rollout/ChatControllerShould.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
public final class ChatControllerShould {
88
@Test
99
void should_use_the_old_chat_system_pulling_messages() {
10-
var controller = new ChatController(0);
10+
var controller = new ChatController(new AlwaysOldChatChatRolloutPercentageRepository());
1111

1212
assertEquals("Messages from the OLD chat system", controller.pullMessages());
1313
}
1414

1515
@Test
1616
void should_use_the_new_chat_system_pulling_messages() {
17-
var controller = new ChatController(100);
17+
var controller = new ChatController(new AlwaysNewChatChatRolloutPercentageRepository());
1818

1919
assertEquals("Messages from the NEW chat system", controller.pullMessages());
2020
}

0 commit comments

Comments
 (0)