/src/test/java/net/rubyeye/xmemcached/test/unittest/impl/AddressMemcachedSessionComparatorUnitTest.java

http://github.com/killme2008/xmemcached · Java · 280 lines · 162 code · 74 blank · 44 comment · 3 complexity · 12d60b46f1b5df51eddd8ca29d05e88d MD5 · raw file

  1. package net.rubyeye.xmemcached.test.unittest.impl;
  2. import java.net.InetAddress;
  3. import java.net.InetSocketAddress;
  4. import java.net.UnknownHostException;
  5. import java.nio.ByteOrder;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.List;
  9. import java.util.concurrent.Future;
  10. import junit.framework.TestCase;
  11. import net.rubyeye.xmemcached.buffer.BufferAllocator;
  12. import net.rubyeye.xmemcached.impl.AddressMemcachedSessionComparator;
  13. import net.rubyeye.xmemcached.networking.MemcachedSession;
  14. import net.rubyeye.xmemcached.utils.InetSocketAddressWrapper;
  15. import com.google.code.yanf4j.core.Handler;
  16. import com.google.code.yanf4j.core.Session;
  17. import com.google.code.yanf4j.core.CodecFactory.Decoder;
  18. import com.google.code.yanf4j.core.CodecFactory.Encoder;
  19. public class AddressMemcachedSessionComparatorUnitTest extends TestCase {
  20. static private class MockSession implements MemcachedSession, Session {
  21. private final InetSocketAddress address;
  22. public InetSocketAddressWrapper getInetSocketAddressWrapper() {
  23. return new InetSocketAddressWrapper(this.address, 0, 0, null);
  24. }
  25. public MockSession(InetSocketAddress address) {
  26. super();
  27. this.address = address;
  28. }
  29. public void quit() {
  30. // TODO Auto-generated method stub
  31. }
  32. public boolean isAuthFailed() {
  33. // TODO Auto-generated method stub
  34. return false;
  35. }
  36. public void setAuthFailed(boolean authFailed) {
  37. // TODO Auto-generated method stub
  38. }
  39. public Future<Boolean> asyncWrite(Object packet) {
  40. // TODO Auto-generated method stub
  41. return null;
  42. }
  43. public void clearAttributes() {
  44. // TODO Auto-generated method stub
  45. }
  46. public void destroy() {
  47. // TODO Auto-generated method stub
  48. }
  49. public void close() {
  50. // TODO Auto-generated method stub
  51. }
  52. public void flush() {
  53. // TODO Auto-generated method stub
  54. }
  55. public Object getAttribute(String key) {
  56. // TODO Auto-generated method stub
  57. return null;
  58. }
  59. public Decoder getDecoder() {
  60. // TODO Auto-generated method stub
  61. return null;
  62. }
  63. public Encoder getEncoder() {
  64. // TODO Auto-generated method stub
  65. return null;
  66. }
  67. public Handler getHandler() {
  68. // TODO Auto-generated method stub
  69. return null;
  70. }
  71. public long getLastOperationTimeStamp() {
  72. // TODO Auto-generated method stub
  73. return 0;
  74. }
  75. public InetAddress getLocalAddress() {
  76. // TODO Auto-generated method stub
  77. return null;
  78. }
  79. public ByteOrder getReadBufferByteOrder() {
  80. // TODO Auto-generated method stub
  81. return null;
  82. }
  83. public InetSocketAddress getRemoteSocketAddress() {
  84. // TODO Auto-generated method stub
  85. return null;
  86. }
  87. public long getScheduleWritenBytes() {
  88. // TODO Auto-generated method stub
  89. return 0;
  90. }
  91. public long getSessionIdleTimeout() {
  92. // TODO Auto-generated method stub
  93. return 0;
  94. }
  95. public long getSessionTimeout() {
  96. // TODO Auto-generated method stub
  97. return 0;
  98. }
  99. public boolean isClosed() {
  100. // TODO Auto-generated method stub
  101. return false;
  102. }
  103. public boolean isExpired() {
  104. // TODO Auto-generated method stub
  105. return false;
  106. }
  107. public boolean isHandleReadWriteConcurrently() {
  108. // TODO Auto-generated method stub
  109. return false;
  110. }
  111. public boolean isIdle() {
  112. // TODO Auto-generated method stub
  113. return false;
  114. }
  115. public boolean isLoopbackConnection() {
  116. // TODO Auto-generated method stub
  117. return false;
  118. }
  119. public boolean isUseBlockingRead() {
  120. // TODO Auto-generated method stub
  121. return false;
  122. }
  123. public boolean isUseBlockingWrite() {
  124. // TODO Auto-generated method stub
  125. return false;
  126. }
  127. public void removeAttribute(String key) {
  128. // TODO Auto-generated method stub
  129. }
  130. public void setAttribute(String key, Object value) {
  131. // TODO Auto-generated method stub
  132. }
  133. public Object setAttributeIfAbsent(String key, Object value) {
  134. // TODO Auto-generated method stub
  135. return null;
  136. }
  137. public void setDecoder(Decoder decoder) {
  138. // TODO Auto-generated method stub
  139. }
  140. public void setEncoder(Encoder encoder) {
  141. // TODO Auto-generated method stub
  142. }
  143. public void setHandleReadWriteConcurrently(boolean handleReadWriteConcurrently) {
  144. // TODO Auto-generated method stub
  145. }
  146. public void setReadBufferByteOrder(ByteOrder readBufferByteOrder) {
  147. // TODO Auto-generated method stub
  148. }
  149. public void setSessionIdleTimeout(long sessionIdleTimeout) {
  150. // TODO Auto-generated method stub
  151. }
  152. public void setSessionTimeout(long sessionTimeout) {
  153. // TODO Auto-generated method stub
  154. }
  155. public void setUseBlockingRead(boolean useBlockingRead) {
  156. // TODO Auto-generated method stub
  157. }
  158. public void setUseBlockingWrite(boolean useBlockingWrite) {
  159. // TODO Auto-generated method stub
  160. }
  161. public void start() {
  162. // TODO Auto-generated method stub
  163. }
  164. public void write(Object packet) {
  165. // TODO Auto-generated method stub
  166. }
  167. public int getOrder() {
  168. // TODO Auto-generated method stub
  169. return 0;
  170. }
  171. public int getWeight() {
  172. // TODO Auto-generated method stub
  173. return 0;
  174. }
  175. public boolean isAllowReconnect() {
  176. // TODO Auto-generated method stub
  177. return false;
  178. }
  179. public void setAllowReconnect(boolean allow) {
  180. // TODO Auto-generated method stub
  181. }
  182. public void setBufferAllocator(BufferAllocator allocator) {
  183. // TODO Auto-generated method stub
  184. }
  185. }
  186. public void testCompare() throws UnknownHostException {
  187. List<Session> sessionList = new ArrayList<Session>();
  188. for (int i = 0; i < 100; i++) {
  189. sessionList.add(new MockSession(new InetSocketAddress("hostA", i)));
  190. sessionList.add(new MockSession(new InetSocketAddress("hostB", i)));
  191. byte[] ipAddr = new byte[] {127, 0, 0, 1};
  192. sessionList.add(new MockSession(new InetSocketAddress(InetAddress.getByAddress(ipAddr), i)));
  193. }
  194. Collections.sort(sessionList, new AddressMemcachedSessionComparator());
  195. for (int i = 0; i < sessionList.size(); i++) {
  196. if (i < sessionList.size() - 1) {
  197. int next = i + 1;
  198. MockSession nextSession = (MockSession) sessionList.get(next);
  199. MockSession currentSession = (MockSession) sessionList.get(i);
  200. assertTrue(currentSession.getInetSocketAddressWrapper().getInetSocketAddress().toString()
  201. .compareTo(
  202. nextSession.getInetSocketAddressWrapper().getInetSocketAddress().toString()) < 0);
  203. }
  204. }
  205. }
  206. }