package com.shinemo.mpush.tools.redis;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Before;
import org.junit.Test;

import com.shinemo.mpush.tools.InetAddressUtil;
import com.shinemo.mpush.tools.redis.manage.RedisGroupManage;
import com.shinemo.mpush.tools.redis.manage.RedisManage;
import com.shinemo.mpush.tools.zk.ServerApp;
import com.shinemo.mpush.tools.zk.manage.ServerManage;

public class RedisGroupManageTest {
	
	ServerApp app = new ServerApp(InetAddressUtil.getInetAddress(),"3000");
	ServerManage manage = new ServerManage(app);
	List<RedisGroup> groupList = null;
	
	RedisNode node = new RedisNode("127.0.0.1", 6379, "ShineMoIpo");
	RedisNode node2 = new RedisNode("127.0.0.1", 6380, "ShineMoIpo");
	
	@Before
	public void init(){
		manage.start();
		groupList = RedisGroupManage.instance.getGroupList();
	}
	
	@Test
	public void testGetRedisGroup(){
		for(RedisGroup group:groupList){
			for(RedisNode node:group.getRedisNodeList()){
				System.out.println(group+ToStringBuilder.reflectionToString(node, ToStringStyle.MULTI_LINE_STYLE));
			}
			
		}
	}
	
	@Test
	public void testAdd(){
		User user = RedisManage.get("huang2", User.class);
		if(user == null){
			user = new User("hi", 10, new Date());
			RedisManage.set("huang2", user);
			user = RedisManage.get("huang2", User.class);
		}
		System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE));
		
		User nowUser = RedisUtil.get(node, "huang2", User.class);
		System.out.println("node1:" + ToStringBuilder.reflectionToString(nowUser));

		nowUser = RedisUtil.get(node2, "huang2", User.class);
		System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser));

		RedisManage.del("huang2");

		nowUser = RedisUtil.get(node2, "huang2", User.class);
		if (nowUser == null) {
			System.out.println("node2 nowUser is null");
		} else {
			System.out.println("node2:" + ToStringBuilder.reflectionToString(nowUser));
		}
		
		nowUser = RedisUtil.get(node, "huang2", User.class);
		System.out.println("node:" + ToStringBuilder.reflectionToString(nowUser));
		
	}
	
	@Test
	public void testPub(){
		User user = new User("pub", 10, new Date());
		RedisManage.publish("channel1", user);
		RedisManage.publish("channel2", user);
	}

	@Test
	public void testSub(){
		RedisManage.subscribe("channel1","channel2");
		try {
			Thread.sleep(Integer.MAX_VALUE);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	

}

@@ -2226,24 +2226,52 @@
  testPub()%7B%0A
+%09%09for(int i = 0;i%3C20;i++)%7B%0A%09
 %09%09User user 
@@ -2286,16 +2286,18 @@
 er(%22pub%22
++i
 , 10, ne
@@ -2305,24 +2305,25 @@
  Date());%0A%09%09
+%09
 RedisManage.
@@ -2349,16 +2349,17 @@
  user);%0A
+%09
 %09%09RedisM
@@ -2391,16 +2391,20 @@
  user);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09@Te

add redis pub sub tset