Skip to content

Commit 7f65ccb

Browse files
committed
Fetch and History withMeta tests
1 parent d7a3394 commit 7f65ccb

File tree

12 files changed

+199
-26
lines changed

12 files changed

+199
-26
lines changed

.pubnub.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,4 +390,4 @@ supported-platforms:
390390
- "Ubuntu 12.04+, with Graphics card DX9 (shader model 3.0) or DX11 with feature level 9.3 capabilities; and CPU SSE2 instruction set support."
391391
- "Mac OS X 10.8+, with Graphics card DX9 (shader model 3.0) or DX11 with feature level 9.3 capabilities; and CPU SSE2 instruction set support."
392392
version: "PubNub Unity SDK"
393-
version: v4.2.1
393+
version: v4.3.0

PubNubUnity/Assets/PubNub/Builders/History/FetchMessagesRequestBuilder.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public FetchMessagesRequestBuilder Count(ushort historyCount){
6464
return this;
6565
}
6666

67-
public FetchMessagesRequestBuilder IncludeMeta(bool withMeta){
68-
IncludeMetaInHistory = withMeta;
67+
public FetchMessagesRequestBuilder IncludeMeta(bool includeMeta){
68+
IncludeMetaInHistory = includeMeta;
6969
return this;
7070
}
7171

@@ -143,6 +143,7 @@ protected override void CreatePubNubResponse(object deSerializedResult, RequestS
143143
pnFetchMessagesResult = null;
144144
pnStatus = base.CreateErrorResponseFromException(ex, requestState, PNStatusCategory.PNUnknownCategory);
145145
}
146+
146147
Callback(pnFetchMessagesResult, pnStatus);
147148
}
148149

@@ -172,6 +173,14 @@ protected bool CreateFetchMessagesResult(object objChannelsDict, out Dictionary<
172173
object objPayload;
173174
messageDataDict.TryGetValue("message", out objPayload);
174175

176+
object meta;
177+
178+
if(messageDataDict.TryGetValue("meta", out meta)){
179+
#if (ENABLE_PUBNUB_LOGGING)
180+
this.PubNubInstance.PNLog.WriteToLog(string.Format ("CreateFetchMessagesResult: meta {0}.", meta.ToString()), PNLoggingMethod.LevelInfo);
181+
#endif
182+
}
183+
175184
object objTimetoken;
176185
messageDataDict.TryGetValue("timetoken", out objTimetoken);
177186
long timetoken;
@@ -191,10 +200,14 @@ protected bool CreateFetchMessagesResult(object objChannelsDict, out Dictionary<
191200
objPayload,
192201
timetoken,
193202
timetoken,
194-
null,
203+
meta,
195204
""
196205
);
197206
lstMessageResult.Add(pnMessageResult);
207+
#if (ENABLE_PUBNUB_LOGGING)
208+
this.PubNubInstance.PNLog.WriteToLog(string.Format ("CreateFetchMessagesResult: pnMessageResult {0}.", pnMessageResult.ToString()), PNLoggingMethod.LevelInfo);
209+
#endif
210+
198211
}
199212
}
200213
channelsResult.Add(channelName, lstMessageResult);

PubNubUnity/Assets/PubNub/Builders/History/HistoryRequestBuilder.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public HistoryRequestBuilder Count(ushort historyCount){
6363
HistoryCount = historyCount;
6464
return this;
6565
}
66-
public HistoryRequestBuilder IncludeMeta(bool withMeta){
67-
IncludeMetaInHistory = withMeta;
66+
public HistoryRequestBuilder IncludeMeta(bool includeMeta){
67+
IncludeMetaInHistory = includeMeta;
6868
return this;
6969
}
7070

@@ -172,12 +172,20 @@ private void ExtractMessageWithTimetokens( object element, string cipherKey, out
172172
#endif
173173

174174
object t;
175-
historyMessage.TryGetValue("timetoken", out t);
176-
pnHistoryItemResult.Timetoken = Utility.ValidateTimetoken(t.ToString(), false);
177-
#if (ENABLE_PUBNUB_LOGGING)
178-
this.PubNubInstance.PNLog.WriteToLog(string.Format ("ExtractMessageWithTimetokens: t {0}", t), PNLoggingMethod.LevelInfo);
179-
#endif
175+
if(historyMessage.TryGetValue("timetoken", out t)){
176+
pnHistoryItemResult.Timetoken = Utility.ValidateTimetoken(t.ToString(), false);
177+
#if (ENABLE_PUBNUB_LOGGING)
178+
this.PubNubInstance.PNLog.WriteToLog(string.Format ("ExtractMessageWithTimetokens: t {0}", t), PNLoggingMethod.LevelInfo);
179+
#endif
180+
}
180181

182+
object m;
183+
if(historyMessage.TryGetValue("meta", out m)){
184+
pnHistoryItemResult.Meta = m;
185+
#if (ENABLE_PUBNUB_LOGGING)
186+
this.PubNubInstance.PNLog.WriteToLog(string.Format ("ExtractMessageWithTimetokens: m {0}", m), PNLoggingMethod.LevelInfo);
187+
#endif
188+
}
181189
}
182190

183191
private void ExtractMessage( object element, string cipherKey, out PNHistoryItemResult pnHistoryItemResult){
@@ -210,7 +218,7 @@ private bool ExtractMessages(object[] historyResponseArray, ref PNHistoryResult
210218
#endif
211219
PNHistoryItemResult pnHistoryItemResult;
212220

213-
if(this.IncludeTimetokenInHistory){
221+
if((this.IncludeTimetokenInHistory) || (this.IncludeMetaInHistory)){
214222
ExtractMessageWithTimetokens(element, this.PubNubInstance.PNConfig.CipherKey, out pnHistoryItemResult);
215223
} else {
216224
ExtractMessage(element, this.PubNubInstance.PNConfig.CipherKey, out pnHistoryItemResult);

PubNubUnity/Assets/PubNub/EndPoints/HIstory/FetchBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public FetchBuilder Count(ushort historyCount){
3939
return this;
4040
}
4141

42-
public FetchBuilder IncludeMeta(bool withMeta){
43-
pubBuilder.IncludeMeta(withMeta);
42+
public FetchBuilder IncludeMeta(bool includeMeta){
43+
pubBuilder.IncludeMeta(includeMeta);
4444
return this;
4545
}
4646

PubNubUnity/Assets/PubNub/EndPoints/HIstory/HistoryBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public HistoryBuilder Count(ushort historyCount){
4242
pubBuilder.Count(historyCount);
4343
return this;
4444
}
45-
public HistoryBuilder IncludeMeta(bool withMeta){
46-
pubBuilder.IncludeMeta(withMeta);
45+
public HistoryBuilder IncludeMeta(bool includeMeta){
46+
pubBuilder.IncludeMeta(includeMeta);
4747
return this;
4848
}
4949

PubNubUnity/Assets/PubNub/Examples/Example.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void ButtonGetPresenceStateHandler(){
153153
}
154154
void ButtonHistoryHandler(){
155155

156-
pubnub.History ().Channel("channel1").IncludeTimetoken(true).Async ((result, status) => {
156+
pubnub.History ().Channel("channel1").IncludeTimetoken(true).IncludeMeta(true).Async ((result, status) => {
157157

158158
if(status.Error){
159159
Debug.Log (string.Format("In Example, History Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category));
@@ -235,7 +235,10 @@ void ButtonPublishHandler(){
235235
Dictionary<string, string> dict = new Dictionary<string, string>();
236236
dict.Add ("k1", "v1");
237237

238-
pubnub.Publish().Channel("channel1").Message("test message" + DateTime.Now.Ticks.ToString()).QueryParam(dict).Async((result, status) => {
238+
Dictionary<string, string> meta = new Dictionary<string, string>();
239+
meta.Add ("k1", "v1");
240+
241+
pubnub.Publish().Channel("channel1").Meta(meta).Message("test message" + DateTime.Now.Ticks.ToString()).QueryParam(dict).Async((result, status) => {
239242
Debug.Log ("in Publish");
240243
if(!status.Error){
241244
Debug.Log (string.Format("DateTime {0}, In Publish Example, Timetoken: {1}", DateTime.UtcNow , result.Timetoken));
@@ -656,7 +659,7 @@ void ListAllChannelsOfGroup(PubNub pubnub, string cg){
656659
}
657660

658661
void FetchMessages(PubNub pubnub, List<string> listChannels){
659-
pubnub.FetchMessages().Channels(listChannels).Async ((result, status) => {
662+
pubnub.FetchMessages().Channels(listChannels).IncludeMeta(true).Async ((result, status) => {
660663
//pubnub.FetchMessages().Channels(new List<string>{"channel2"}).Async ((result, status) => {
661664
if(status.Error){
662665
Debug.Log (string.Format("In Example, FetchMessages Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category));

PubNubUnity/Assets/PubNub/Helpers/Helpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ public static bool TryAddToSubscribeMessageList (object dictObject, ref List<Sub
510510
"publishMetadata region: {11},\n" +
511511
"userMetadata {12} \n" +
512512
"log {13} \n",
513-
"messageType {14}\n"
513+
"messageType {14}\n",
514514

515515
shard,
516516
subscriptionMatch,

PubNubUnity/Assets/PubNub/Models/Consumer/History/PNHistoryResult.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class PNHistoryItemResult {
1414

1515
public long Timetoken { get; set;}
1616
public object Entry { get; set;}
17+
public object Meta { get; set;}
1718

1819
}
1920
}

PubNubUnity/Assets/PubNub/PlayModeTests/PlayModeCommon.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static PNConfiguration SetPNConfig(bool useCipher){
7777
if(useCipher){
7878
pnConfiguration.CipherKey = "enigma";
7979
}
80-
pnConfiguration.LogVerbosity = PNLogVerbosity.NONE;
80+
pnConfiguration.LogVerbosity = PNLogVerbosity.BODY;
8181
pnConfiguration.PresenceTimeout = 60;
8282
pnConfiguration.PresenceInterval= 30;
8383
pnConfiguration.Secure = SslOn;

PubNubUnity/Assets/PubNub/PlayModeTests/PlayModeTests.cs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,7 @@ public IEnumerator TestPublishWithMeta() {
15131513
Assert.True(!result.Timetoken.Equals(0));
15141514
Assert.True(status.Error.Equals(false));
15151515
Assert.True(status.StatusCode.Equals(0), status.StatusCode.ToString());
1516+
Assert.True(!result.Timetoken.Equals(0));
15161517
});
15171518
yield return new WaitForSeconds (PlayModeCommon.WaitTimeBetweenCalls3);
15181519
Assert.True(tresult, "test didn't return");
@@ -1615,6 +1616,145 @@ public IEnumerator TestPublishAndHistory() {
16151616
pubnub.CleanUp();
16161617
}
16171618

1619+
[UnityTest]
1620+
public IEnumerator TestPublishHistoryAndFetchWithMetaAndTT(){
1621+
return PublishHistoryAndFetchWithMetaCommon(true, true);
1622+
}
1623+
1624+
[UnityTest]
1625+
public IEnumerator TestPublishHistoryAndFetchWithMetaWithoutTT(){
1626+
return PublishHistoryAndFetchWithMetaCommon(true, false);
1627+
}
1628+
1629+
[UnityTest]
1630+
public IEnumerator TestPublishHistoryAndFetchWithTTWithoutMeta(){
1631+
return PublishHistoryAndFetchWithMetaCommon(false, true);
1632+
}
1633+
1634+
[UnityTest]
1635+
public IEnumerator TestPublishHistoryAndFetchWithoutMetaAndTT(){
1636+
return PublishHistoryAndFetchWithMetaCommon(false, false);
1637+
}
1638+
1639+
public IEnumerator PublishHistoryAndFetchWithMetaCommon(bool withMeta, bool withTimetoken) {
1640+
PNConfiguration pnConfiguration = PlayModeCommon.SetPNConfig(false);
1641+
System.Random r = new System.Random ();
1642+
pnConfiguration.UUID = "UnityTestConnectedUUID_" + r.Next (100);
1643+
string channel = "UnityPublishAndHistoryChannel" + r.Next (100);;
1644+
string payload = string.Format("payload {0}", pnConfiguration.UUID);
1645+
1646+
PubNub pubnub = new PubNub(pnConfiguration);
1647+
1648+
List<string> channelList2 = new List<string>();
1649+
channelList2.Add(channel);
1650+
bool tresult = false;
1651+
Dictionary<string, string> metaDict = new Dictionary<string, string>();
1652+
metaDict.Add("region", "east");
1653+
long retTT =0;
1654+
1655+
pubnub.Publish().Channel(channel).Meta(metaDict).Message(payload).Async((result, status) => {
1656+
bool timetokenMatch = !result.Timetoken.Equals(0);
1657+
bool statusError = status.Error.Equals(false);
1658+
bool statusCodeMatch = status.StatusCode.Equals(0);
1659+
retTT = result.Timetoken;
1660+
Assert.True(timetokenMatch);
1661+
Assert.True(statusError);
1662+
Assert.True(statusCodeMatch, status.StatusCode.ToString());
1663+
tresult = statusCodeMatch && statusError && timetokenMatch;
1664+
});
1665+
yield return new WaitForSeconds (PlayModeCommon.WaitTimeBetweenCalls3);
1666+
Assert.True(tresult, "test didnt return 1");
1667+
1668+
tresult = false;
1669+
bool tresultMeta = false;
1670+
bool tresultTimetoken = false;
1671+
pubnub.History().Channel(channel).IncludeMeta(withMeta).IncludeTimetoken(withTimetoken).Count(1).Async((result, status) => {
1672+
Assert.True(status.Error.Equals(false));
1673+
if(!status.Error){
1674+
1675+
if((result.Messages!=null) && (result.Messages.Count>0)){
1676+
PNHistoryItemResult pnHistoryItemResult = result.Messages[0] as PNHistoryItemResult;
1677+
Debug.Log("result.Messages[0]" + result.Messages[0].ToString());
1678+
if(pnHistoryItemResult != null){
1679+
tresult = pnHistoryItemResult.Entry.ToString().Contains(payload);
1680+
1681+
if(withMeta){
1682+
Dictionary<string, object> metaDataDict = pnHistoryItemResult.Meta as Dictionary<string, object>;
1683+
object region;
1684+
metaDataDict.TryGetValue("region", out region);
1685+
tresultMeta = region.ToString().Equals("east");
1686+
} else {
1687+
tresultMeta = true;
1688+
}
1689+
if(withTimetoken){
1690+
tresultTimetoken = retTT.Equals(pnHistoryItemResult.Timetoken);
1691+
} else {
1692+
tresultTimetoken = true;
1693+
}
1694+
} else {
1695+
tresult = false;
1696+
tresultMeta = false;
1697+
}
1698+
} else {
1699+
tresult = false;
1700+
}
1701+
1702+
}
1703+
});
1704+
yield return new WaitForSeconds (PlayModeCommon.WaitTimeBetweenCalls3);
1705+
Assert.True(tresult, "test didnt return 2");
1706+
Assert.True(tresultMeta, "test meta didnt return");
1707+
Assert.True(tresultTimetoken, "tresultTimetoken didnt return");
1708+
1709+
tresult = false;
1710+
tresultMeta = false;
1711+
pubnub.FetchMessages().Channels(channelList2).IncludeMeta(withMeta).Async((result, status) => {
1712+
if(!status.Error){
1713+
if(result.Channels != null){
1714+
Dictionary<string, List<PNMessageResult>> fetchResult = result.Channels as Dictionary<string, List<PNMessageResult>>;
1715+
Debug.Log("fetchResult.Count:" + fetchResult.Count);
1716+
foreach(KeyValuePair<string, List<PNMessageResult>> kvp in fetchResult){
1717+
Debug.Log("Channel:" + kvp.Key);
1718+
if(kvp.Key.Equals(channel)){
1719+
1720+
foreach(PNMessageResult msg in kvp.Value){
1721+
Debug.Log("msg.Channel:" + msg.Channel);
1722+
Debug.Log("msg.Payload.ToString():" + msg.Payload.ToString());
1723+
if(msg.Channel.Equals(channel) && (msg.Payload.ToString().Equals(payload))){
1724+
tresult = true;
1725+
}
1726+
if(withMeta){
1727+
Dictionary<string, object> metaDataDict = msg.UserMetadata as Dictionary<string, object>;
1728+
object region;
1729+
if(metaDataDict!=null){
1730+
metaDataDict.TryGetValue("region", out region);
1731+
tresultMeta = region.ToString().Equals("east");
1732+
} else {
1733+
Debug.Log("metaDataDict null" + msg.UserMetadata);
1734+
}
1735+
} else {
1736+
tresultMeta = true;
1737+
}
1738+
1739+
}
1740+
if(!tresult && !tresultMeta){
1741+
break;
1742+
}
1743+
}
1744+
}
1745+
}
1746+
1747+
}
1748+
1749+
});
1750+
yield return new WaitForSeconds (PlayModeCommon.WaitTimeBetweenCalls3);
1751+
Assert.True(tresult, "test didnt return for fetch");
1752+
Assert.True(tresultMeta, "test meta didnt return for fetch");
1753+
1754+
1755+
pubnub.CleanUp();
1756+
}
1757+
16181758
[UnityTest]
16191759
public IEnumerator TestPublishNoStore() {
16201760
PNConfiguration pnConfiguration = PlayModeCommon.SetPNConfig(false);

0 commit comments

Comments
 (0)