@Test public void testRemoveAndWriteAllReentrance() { EmbeddedChannel channel = new EmbeddedChannel(new ChannelInboundHandlerAdapter()); final PendingWriteQueue queue = new PendingWriteQueue(channel.pipeline().firstContext()); ChannelPromise promise = channel.newPromise(); promise.addListener( new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { queue.removeAndWriteAll(); } }); queue.add(1L, promise); ChannelPromise promise2 = channel.newPromise(); queue.add(2L, promise2); queue.removeAndWriteAll(); channel.flush(); assertTrue(promise.isSuccess()); assertTrue(promise2.isSuccess()); assertTrue(channel.finish()); assertEquals(1L, channel.readOutbound()); assertEquals(2L, channel.readOutbound()); assertNull(channel.readOutbound()); assertNull(channel.readInbound()); }
// See https://github.com/netty/netty/issues/3967 @Test public void testCloseChannelOnCreation() { EmbeddedChannel channel = new EmbeddedChannel(); channel.close().syncUninterruptibly(); final PendingWriteQueue queue = new PendingWriteQueue(channel.pipeline().firstContext()); IllegalStateException ex = new IllegalStateException(); ChannelPromise promise = channel.newPromise(); queue.add(1L, promise); queue.removeAndFailAll(ex); assertSame(ex, promise.cause()); }
private void handleLine(String line) { if (line.startsWith("PING ")) { sendRaw("PONG " + line.substring(5), true); return; } String[] split = line.split(" ", 4); String source = split[0].substring(1); String operation = split[1]; // String target = split[2]; // String data = split[3].startsWith(":") ? split[3].substring(1) : split[3]; if (operation.equals("433")) { this.sendRaw("NICK " + this.nickName + "_", true); } else if (operation.equals("004")) { this.connected = true; while (!this.loginMessageQueue.isEmpty()) { this.sendRaw(this.loginMessageQueue.poll()); } if (this.nickservPassword != null && !this.nickservPassword.isEmpty()) { this.sendRaw("NICKSERV IDENTIFY " + this.nickservPassword); } this.connectPromise.setSuccess(); } else if (operation.equals("353")) { String[] users = line.split(":", 3)[2].split(" "); Conversation channel = this.joinedChannels.get(line.split("@", 2)[1].trim().split(" ", 2)[0]); for (String s : users) { String mode = ""; if (s.startsWith("@")) { mode = "o"; s = s.substring(1); } else if (s.startsWith("+")) { mode = "v"; s = s.substring(1); } else if (s.startsWith("%")) { mode = "h"; s = s.substring(1); } channel.onJoin(s, mode); } } else if (operation.equals("JOIN") && source.startsWith(this.getNickName())) { String channel = split[2].substring(1); if (this.joiningChannels.containsKey(channel)) { ChannelPromise promise = this.joiningChannels.get(channel); promise.setSuccess(); this.joiningChannels.remove(channel); } } else if (operation.equals("PRIVMSG")) { String channel = split[2]; if (this.joinedChannels.containsKey(channel)) { Conversation conversation = this.joinedChannels.get(channel); conversation.onMessage(line.substring(1).split("!", 2)[0], line.split(":", 3)[2]); } } else if (operation.equals("JOIN")) { this.joinedChannels.get(split[2].substring(1)).onJoin(source.split("!", 2)[0]); } else if (operation.equals("PART")) { String nick = source.split("!", 2)[0]; String message = line.split(":", 3)[2]; this.joinedChannels.get(split[2]).onPart(nick, message); } else if (operation.equals("KICK")) { String nick = line.split(" ", 5)[3]; String message = line.split(" ", 5)[4].substring(1); this.joinedChannels.get(split[2]).onKick(nick, message); } else if (operation.equals("MODE")) { if (split[2].startsWith("+") || split[2].startsWith("-")) { this.joinedChannels.get(split[2]).onMode(line.split(" ", 5)[4], line.split(" ", 5)[3]); } } else if (operation.equals("NICK")) { String oldNick = source.split("!", 2)[0]; String newNick = split[2].substring(1); for (Conversation channel : this.joinedChannels.values()) { channel.onNick(oldNick, newNick); } } }