only load channels when needed

This commit is contained in:
Carsten Otto
2021-11-19 18:41:48 +01:00
parent 256c1cc42a
commit 9e0f4e58aa
2 changed files with 25 additions and 5 deletions

View File

@@ -16,6 +16,7 @@ import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -100,15 +101,15 @@ public class ChannelService {
}
public Stream<LocalChannel> getAllLocalChannels() {
Set<LocalOpenChannel> openChannels = getOpenChannels();
Set<WaitingCloseChannel> waitingCloseChannels = getWaitingCloseChannels();
Set<ForceClosingChannel> forceClosingChannels = getForceClosingChannels();
Set<ClosedChannel> closedChannels = getClosedChannels();
Supplier<Set<LocalOpenChannel>> openChannels = this::getOpenChannels;
Supplier<Set<ClosedChannel>> closedChannels = this::getClosedChannels;
Supplier<Set<WaitingCloseChannel>> waitingCloseChannels = this::getWaitingCloseChannels;
Supplier<Set<ForceClosingChannel>> forceClosingChannels = this::getForceClosingChannels;
return Stream.of(
openChannels,
closedChannels,
waitingCloseChannels,
forceClosingChannels
).flatMap(Collection::stream);
).map(Supplier::get).flatMap(Collection::stream);
}
}

View File

@@ -29,6 +29,8 @@ import static de.cotto.lndmanagej.model.WaitingCloseChannelFixtures.WAITING_CLOS
import static de.cotto.lndmanagej.model.WaitingCloseChannelFixtures.WAITING_CLOSE_CHANNEL_2;
import static de.cotto.lndmanagej.model.WaitingCloseChannelFixtures.WAITING_CLOSE_CHANNEL_TO_NODE_3;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@@ -65,6 +67,15 @@ class ChannelServiceTest {
assertThat(channelService.getLocalChannel(CHANNEL_ID)).contains(LOCAL_OPEN_CHANNEL);
}
@Test
void getLocalChannel_open_tried_first() {
when(grpcChannels.getChannels()).thenReturn(Set.of(LOCAL_OPEN_CHANNEL));
assertThat(channelService.getLocalChannel(CHANNEL_ID)).contains(LOCAL_OPEN_CHANNEL);
verify(grpcClosedChannels, never()).getClosedChannels();
verify(grpcChannels, never()).getForceClosingChannels();
verify(grpcChannels, never()).getWaitingCloseChannels();
}
@Test
void getLocalChannel_waiting_close_channel() {
when(grpcChannels.getWaitingCloseChannels()).thenReturn(Set.of(WAITING_CLOSE_CHANNEL, WAITING_CLOSE_CHANNEL_2));
@@ -83,6 +94,14 @@ class ChannelServiceTest {
assertThat(channelService.getLocalChannel(CHANNEL_ID)).contains(CLOSED_CHANNEL);
}
@Test
void getLocalChannel_closed_tried_second() {
when(grpcClosedChannels.getClosedChannels()).thenReturn(Set.of(CLOSED_CHANNEL));
assertThat(channelService.getLocalChannel(CHANNEL_ID)).contains(CLOSED_CHANNEL);
verify(grpcChannels, never()).getForceClosingChannels();
verify(grpcChannels, never()).getWaitingCloseChannels();
}
@Test
void getOpenChannel() {
when(grpcChannels.getChannel(CHANNEL_ID_2)).thenReturn(Optional.of(LOCAL_OPEN_CHANNEL));