3
3
import java.io.IOException;
4
4
import java.net.InetSocketAddress;
5
5
import java.net.ServerSocket;
6
import java.nio.ByteBuffer;
6
7
import java.nio.channels.SelectableChannel;
8
import java.util.Arrays;
8
10
import junit.framework.Assert;
9
11
import gearmanij.Constants;
101
public void getRequestAsByteBufferTest() {
103
public void getRequestAsByteBufferTest() throws IOException {
104
GearmanEchoResponseHandler handler = new GearmanEchoResponseHandler();
105
Packet request = new Packet(PacketMagic.REQ,PacketType.ECHO_REQ,new byte [0]);
106
GearmanServerRequest gsr = new GearmanServerRequest(handler,request);
107
ByteBuffer bb = gsr.getRequestAsByteBuffer();
108
ByteBuffer rb = request.toByteBuffer();
109
Assert.assertEquals("limit of byte buffer return by getRequestAsByteBuffer does not match that of request packet.",rb.limit(), bb.limit());
110
byte [] bba = new byte [bb.limit()];
111
byte [] rba = new byte [rb.limit()];
112
bb.get(bba, 0, bb.limit());
113
rb.get(rba, 0, rb.limit());
114
Assert.assertTrue("contents of byte buffer returned by getRequestAsByteBuffer does not match content of request.", Arrays.equals(rba, bba));
106
118
public void addResponsePacketTest() {
119
GearmanEchoResponseHandler handler = new GearmanEchoResponseHandler();
120
Packet request = new Packet(PacketMagic.REQ,PacketType.ECHO_REQ,new byte [0]);
121
GearmanServerRequest gsr = new GearmanServerRequest(handler,request);
124
gsr.addResponsePacket(null);
125
Assert.fail("Attempt to add a null repsonse to GearmanServerRequest did not result an exception being raised");
126
} catch (Throwable t) {
127
Assert.assertTrue("Attempt to add a null repsonse to GearmanServerRequest resulted in an unexpected exception being raised", t instanceof IllegalArgumentException);
130
gsr.addResponsePacket(request);
131
Assert.fail("Attempt to add an invalid repsonse packet to GearmanServerRequest did not result an exception being raised");
132
} catch (Throwable t) {
133
Assert.assertTrue("Attempt to iadd nvalid repsonse packet to GearmanServerRequest resulted in an unexpected exception being raised", t instanceof IllegalArgumentException);
111
138
public void getHandlerTest() {
139
GearmanEchoResponseHandler handler = new GearmanEchoResponseHandler();
140
Packet request = new Packet(PacketMagic.REQ,PacketType.ECHO_REQ,new byte [0]);
141
GearmanServerRequest gsr = new GearmanServerRequest(handler,request);
142
Assert.assertEquals("handler returned by getHandler does not match handler passed into constructor.",handler, gsr.getHandler());