Skip to content

Integration test improvements + Reconnect() tweak #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
---
version: v9.0.0
version: v9.0.1
changelog:
- date: 2025-05-30
version: v9.0.1
changes:
- type: feature
text: "Improved Unity-side testing of core C# codebase."
- type: bug
text: "Updated the legacy Reconnect() call to properly call new C#-side signature."
- date: 2025-05-08
version: v9.0.0
changes:
Expand Down Expand Up @@ -772,7 +779,7 @@ sdks:
distribution-type: package
distribution-repository: git release
package-name: PubNub.unitypackage
location: https://github.com/pubnub/unity/releases/download/v9.0.0/PubNub.unitypackage
location: https://github.com/pubnub/unity/releases/download/v9.0.1/PubNub.unitypackage
requires:
-
name: "UnityEditor"
Expand Down Expand Up @@ -904,7 +911,7 @@ sdks:
distribution-type: package
distribution-repository: git release
package-name: PubNub.unitypackage
location: https://github.com/pubnub/unity/releases/download/v9.0.0/PubNub.unitypackage
location: https://github.com/pubnub/unity/releases/download/v9.0.1/PubNub.unitypackage
requires:
-
name: "UnityEditor"
Expand Down
2 changes: 1 addition & 1 deletion PubNubUnity/Assets/PubNub/Runtime/Adapters/PubNub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class PubnubExtensions {
public static ISubscribeOperation<string> Subscribe(this Pubnub pn) => pn.Subscribe<string>();

[Obsolete("Use the generic version instead")]
public static bool Reconnect(this Pubnub pn) => pn.Reconnect<string>();
public static bool Reconnect(this Pubnub pn) => pn.Reconnect<string>().Result;

/// <summary>
/// Add an event listener that dispatches to the main Unity thread. This allows manipulation of the built-in classes within callbacks.
Expand Down
Binary file modified PubNubUnity/Assets/PubNub/Runtime/Plugins/PubnubApiUnity.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace PubnubApi.Unity
{
public class UnityPNSDKSource : IPNSDKSource {

private const string build = "9.0.0";
private const string build = "9.0.1";

public string GetPNSDK() {
#if(UNITY_IOS)
Expand Down
24 changes: 19 additions & 5 deletions PubNubUnity/Assets/PubNub/Tests/PNTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,32 @@
using UnityEngine;

namespace PubnubApi.Unity.Tests {

public static class TestUtils {
public static IEnumerator AsCoroutine(this Task task)
{
while (!task.IsCompleted) yield return null;
// if task is faulted, throws the exception
task.GetAwaiter().GetResult();
}
}

public class PNTestBase {
protected static Pubnub pn;
protected static SubscribeCallbackListener listener = new();
protected static PNConfiguration configuration;

[OneTimeSetUp]
public void OneTimeSetUp() {
PNConfiguration pnConfiguration = new PNConfiguration(new UserId(System.Guid.NewGuid().ToString())) {
PublishKey = System.Environment.GetEnvironmentVariable("PUB_KEY"),
SubscribeKey = System.Environment.GetEnvironmentVariable("SUB_KEY"),
SecretKey = System.Environment.GetEnvironmentVariable("PAM_SECRET_KEY")
var envPub = System.Environment.GetEnvironmentVariable("PUB_KEY");
var envSub = System.Environment.GetEnvironmentVariable("SUB_KEY");
var envSec = System.Environment.GetEnvironmentVariable("PAM_SECRET_KEY");
configuration = new PNConfiguration(new UserId(System.Guid.NewGuid().ToString())) {
PublishKey = string.IsNullOrEmpty(envPub) ? "demo-36" : envPub,
SubscribeKey = string.IsNullOrEmpty(envSub) ? "demo-36" : envSub,
SecretKey = envSec ?? "demo-36"
};
pn = new Pubnub(pnConfiguration);
pn = new Pubnub(configuration);

pn.AddListener(listener);

Expand Down
68 changes: 67 additions & 1 deletion PubNubUnity/Assets/PubNub/Tests/Publish.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using NUnit.Framework;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.TestTools;

namespace PubnubApi.Unity.Tests {

public class DummyCustomClass {
public string someText;
public int someInt;
public List<int> someCollection;
}

public class Publish : PNTestBase {
string lastMessage = null;

Expand Down Expand Up @@ -39,6 +50,61 @@ public IEnumerator ReceiveMessage() {
lastMessage = null;
}

[UnityTest]
public IEnumerator PublishAndReceiveCustomMessageWithUnityJson() {
var mainThread = Thread.CurrentThread;
yield return TestTask().AsCoroutine();
async Task TestTask() {
var randomChannelId = $"unity_test_{Guid.NewGuid()}";

pn.Subscribe<DummyCustomClass>().Channels(new []{randomChannelId}).Execute();
await Task.Delay(3000);

var correctMessage = false;
var correctThread = false;

var receivedTaskSource = new TaskCompletionSource<bool>();
var receiveCancellation = new CancellationTokenSource(15000);
receiveCancellation.Token.Register(() => receivedTaskSource.TrySetCanceled(), useSynchronizationContext: false);

var messageDelegate = new Action<Pubnub,PNMessageResult<object>>(delegate(Pubnub p, PNMessageResult<object> message) {
if (Thread.CurrentThread.Equals(mainThread)) {
correctThread = true;
}
if (message.Message is DummyCustomClass dummyClassObject
&& dummyClassObject.someCollection.SequenceEqual(new List<int>() { 2, 1, 3, 7 })
&& dummyClassObject.someText == "hello there"
&& dummyClassObject.someInt == 97) {
correctMessage = true;
}
receivedTaskSource.TrySetResult(true);
});
listener.onMessage += messageDelegate;

var publishResult = await pn.Publish().Channel(randomChannelId).Message(new DummyCustomClass() {
someCollection = new List<int>() { 2, 1, 3, 7 },
someInt = 97,
someText = "hello there"
}).ExecuteAsync();

Assert.IsNotNull(publishResult.Result, "publishResult.Result should not be null");
Assert.IsNotNull(publishResult.Status, "publishResult.Status should not be null");
Assert.IsFalse(publishResult.Status.Error, $"publishResult.Status.Error is true, error: {publishResult.Status.ErrorData?.Information}");

var received = true;
try {
await receivedTaskSource.Task.ConfigureAwait(false);
} catch (TaskCanceledException e) {
received = false;
}
Assert.IsTrue(received, "didn't receive message callback");
Assert.IsTrue(correctThread, "callback was dispatched on wrong thread");
Assert.IsTrue(correctMessage, "deserialized message had incorrect data");

listener.onMessage -= messageDelegate;
}
}

[TearDown]
public void TearDown() {
listener.onMessage -= OnMessage;
Expand Down
2 changes: 1 addition & 1 deletion PubNubUnity/Assets/PubNub/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.pubnub.sdk",
"version": "9.0.0",
"version": "9.0.1",
"displayName": "PubNub SDK",
"description": "PubNub Real-time Cloud-Hosted Push API and Push Notification Client Frameworks",
"unity": "2018.2",
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9.0.0
9.0.1
Loading