|
| 1 | +package com.pubnub.api; |
| 2 | + |
| 3 | +import org.json.JSONArray; |
| 4 | +import org.json.JSONException; |
| 5 | +import org.json.JSONObject; |
| 6 | +import org.junit.Before; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.util.concurrent.CountDownLatch; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | + |
| 12 | +import static com.pubnub.api.matchers.JSONAssert.assertJSONArrayHas; |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | + |
| 15 | +public class HereNowTest { |
| 16 | + Pubnub pubnub; |
| 17 | + String group; |
| 18 | + String auth_key; |
| 19 | + |
| 20 | + double random; |
| 21 | + |
| 22 | + @Before |
| 23 | + public void setUp() throws InterruptedException { |
| 24 | + random = Math.random(); |
| 25 | + |
| 26 | + pubnub = new Pubnub("demo", "demo", "demo"); |
| 27 | + pubnub.setOrigin("dara24.devbuild"); |
| 28 | + pubnub.setCacheBusting(false); |
| 29 | + |
| 30 | + group = "jtest-" + random; |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testHereNowForOneChannel() |
| 35 | + throws InterruptedException, PubnubException, JSONException { |
| 36 | + |
| 37 | + final String channel = "ch1" + random; |
| 38 | + |
| 39 | + final CountDownLatch latch1 = new CountDownLatch(1); |
| 40 | + final CountDownLatch latch2 = new CountDownLatch(1); |
| 41 | + |
| 42 | + final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1) { |
| 43 | + @Override |
| 44 | + public void connectCallback(String item, Object message) { |
| 45 | + if (item.equals(channel)) latch.countDown(); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2); |
| 50 | + |
| 51 | + pubnub.subscribe(channel, cb1); |
| 52 | + latch1.await(10, TimeUnit.SECONDS); |
| 53 | + Thread.sleep(1000); |
| 54 | + |
| 55 | + pubnub.hereNow(channel, cb2); |
| 56 | + latch2.await(10, TimeUnit.SECONDS); |
| 57 | + |
| 58 | + JSONObject response = (JSONObject) cb2.getResponse(); |
| 59 | + JSONArray uuids = response.getJSONArray("uuids"); |
| 60 | + assertJSONArrayHas(pubnub.getUUID(), uuids); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testHereNowForOneChannelGroup() |
| 65 | + throws InterruptedException, PubnubException, JSONException { |
| 66 | + |
| 67 | + final String[] channels = new String[]{"ch1" + random, "ch2" + random}; |
| 68 | + |
| 69 | + final CountDownLatch latch1 = new CountDownLatch(1); |
| 70 | + final CountDownLatch latch2 = new CountDownLatch(1); |
| 71 | + final CountDownLatch latch3 = new CountDownLatch(1); |
| 72 | + |
| 73 | + final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1); |
| 74 | + final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2) { |
| 75 | + @Override |
| 76 | + public void connectCallback(String item, Object message) { |
| 77 | + latch.countDown(); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3); |
| 82 | + |
| 83 | + pubnub.addChannelToGroup(group, channels, cb1); |
| 84 | + latch1.await(10, TimeUnit.SECONDS); |
| 85 | + |
| 86 | + pubnub.subscribe(channels[0], cb2); |
| 87 | + latch2.await(10, TimeUnit.SECONDS); |
| 88 | + Thread.sleep(1000); |
| 89 | + |
| 90 | + pubnub.hereNowGroup(group, cb3); |
| 91 | + latch3.await(10, TimeUnit.SECONDS); |
| 92 | + |
| 93 | + JSONObject response = (JSONObject) cb3.getResponse(); |
| 94 | + JSONArray uuids = response |
| 95 | + .getJSONObject("channels") |
| 96 | + .getJSONObject(channels[0]) |
| 97 | + .getJSONArray("uuids"); |
| 98 | + |
| 99 | + assertEquals(1, response.getInt("total_occupancy")); |
| 100 | + assertEquals(1, response.getInt("total_channels")); |
| 101 | + |
| 102 | + assertJSONArrayHas(pubnub.getUUID(), uuids); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testHereNowForMultipleChannels() throws InterruptedException, PubnubException, JSONException { |
| 107 | + final String[] channels = new String[]{"ch1" + random, "ch2" + random}; |
| 108 | + final String[] groups = new String[]{group, "jtest2-" + random}; |
| 109 | + |
| 110 | + final CountDownLatch latch1 = new CountDownLatch(1); |
| 111 | + final CountDownLatch latch2 = new CountDownLatch(1); |
| 112 | + final CountDownLatch latch3 = new CountDownLatch(1); |
| 113 | + final CountDownLatch latch4 = new CountDownLatch(1); |
| 114 | + |
| 115 | + final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1); |
| 116 | + final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2); |
| 117 | + final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3) { |
| 118 | + @Override |
| 119 | + public void connectCallback(String item, Object message) { |
| 120 | + latch.countDown(); |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + final TestHelper.SimpleCallback cb4 = new TestHelper.SimpleCallback(latch4); |
| 125 | + |
| 126 | + pubnub.addChannelToGroup(groups[0], channels, cb1); |
| 127 | + pubnub.addChannelToGroup(groups[1], channels, cb2); |
| 128 | + latch1.await(10, TimeUnit.SECONDS); |
| 129 | + latch2.await(10, TimeUnit.SECONDS); |
| 130 | + |
| 131 | + pubnub.subscribe(channels, cb3); |
| 132 | + latch3.await(10, TimeUnit.SECONDS); |
| 133 | + Thread.sleep(1000); |
| 134 | + |
| 135 | + pubnub.hereNowGroup(group, cb4); |
| 136 | + latch4.await(10, TimeUnit.SECONDS); |
| 137 | + |
| 138 | + JSONObject response = (JSONObject) cb4.getResponse(); |
| 139 | + JSONArray uuids1 = response |
| 140 | + .getJSONObject("channels") |
| 141 | + .getJSONObject(channels[0]) |
| 142 | + .getJSONArray("uuids"); |
| 143 | + |
| 144 | + JSONArray uuids2 = response |
| 145 | + .getJSONObject("channels") |
| 146 | + .getJSONObject(channels[1]) |
| 147 | + .getJSONArray("uuids"); |
| 148 | + |
| 149 | + assertEquals(2, response.getInt("total_occupancy")); |
| 150 | + assertEquals(2, response.getInt("total_channels")); |
| 151 | + |
| 152 | + assertJSONArrayHas(pubnub.getUUID(), uuids1); |
| 153 | + assertJSONArrayHas(pubnub.getUUID(), uuids2); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testHereNowGlobal() throws JSONException, InterruptedException, PubnubException { |
| 158 | + final String[] channels = new String[]{"ch1-" + random, "ch2-" + random}; |
| 159 | + |
| 160 | + Pubnub pubnub2 = new Pubnub("demo", "demo"); |
| 161 | + pubnub2.setOrigin("dara24.devbuild"); |
| 162 | + pubnub2.setCacheBusting(false); |
| 163 | + |
| 164 | + final CountDownLatch latch3 = new CountDownLatch(1); |
| 165 | + final CountDownLatch latch4 = new CountDownLatch(1); |
| 166 | + final CountDownLatch latch5 = new CountDownLatch(1); |
| 167 | + |
| 168 | + final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3) { |
| 169 | + @Override |
| 170 | + public void connectCallback(String item, Object message) { |
| 171 | + latch.countDown(); |
| 172 | + } |
| 173 | + }; |
| 174 | + |
| 175 | + final TestHelper.SimpleCallback cb4 = new TestHelper.SimpleCallback(latch4) { |
| 176 | + @Override |
| 177 | + public void connectCallback(String item, Object message) { |
| 178 | + latch.countDown(); |
| 179 | + } |
| 180 | + }; |
| 181 | + |
| 182 | + final TestHelper.SimpleCallback cb5 = new TestHelper.SimpleCallback(latch5); |
| 183 | + |
| 184 | + pubnub.subscribe(channels[0], cb3); |
| 185 | + latch3.await(10, TimeUnit.SECONDS); |
| 186 | + pubnub2.subscribe(channels[1], cb4); |
| 187 | + latch4.await(10, TimeUnit.SECONDS); |
| 188 | + Thread.sleep(1000); |
| 189 | + |
| 190 | + pubnub.hereNow(true, true, cb5); |
| 191 | + latch5.await(10, TimeUnit.SECONDS); |
| 192 | + |
| 193 | + JSONObject response = (JSONObject) cb5.getResponse(); |
| 194 | + |
| 195 | + String uuid1 = response |
| 196 | + .getJSONObject("channels") |
| 197 | + .getJSONObject(channels[0]) |
| 198 | + .getJSONArray("uuids") |
| 199 | + .getJSONObject(0) |
| 200 | + .getString("uuid"); |
| 201 | + |
| 202 | + String uuid2 = response |
| 203 | + .getJSONObject("channels") |
| 204 | + .getJSONObject(channels[1]) |
| 205 | + .getJSONArray("uuids") |
| 206 | + .getJSONObject(0) |
| 207 | + .getString("uuid"); |
| 208 | + |
| 209 | + assertEquals(pubnub.getUUID(), uuid1); |
| 210 | + assertEquals(pubnub2.getUUID(), uuid2); |
| 211 | + } |
| 212 | +} |
0 commit comments