Exemplo n.º 1
0
        public IEnumerator TestRemoveImmediate()
        {
            var cacher = new DummyCacher();
            var agent  = new CacherAgent <string, DummyCacherData>(cacher);

            Assert.AreEqual(cacher, agent.Cacher);

            agent.UseDelayedRemove = false;
            agent.RemoveDelay      = 0f;
            Assert.IsNull(agent.Listener);
            Assert.IsFalse(cacher.IsCached("A"));

            agent.Request("A");
            Assert.IsFalse(cacher.IsCached("A"));
            Assert.IsNull(agent.Listener.Value);
            yield return(new WaitForSecondsRealtime(1.5f));

            var value = agent.Listener.Value;

            Assert.IsTrue(cacher.IsCached("A"));
            Assert.IsNotNull(value);
            Assert.IsFalse(value.IsDestroyed);

            agent.Remove();
            Assert.IsFalse(cacher.IsCached("A"));
            Assert.IsNull(agent.Listener);
            Assert.IsTrue(value.IsDestroyed);
        }
Exemplo n.º 2
0
        public IEnumerator TestRemove()
        {
            var cacher = new DummyCacher();

            var listener  = cacher.Request("AA");
            var listener2 = cacher.Request("BB");
            var listener3 = cacher.Request("CC");

            Assert.IsFalse(cacher.IsCached("AA"));
            Assert.IsFalse(cacher.IsCached("BB"));
            Assert.IsFalse(cacher.IsCached("CC"));

            yield return(new WaitForSeconds(0.5f));

            cacher.Remove(listener);

            yield return(new WaitForSeconds(2f));

            Assert.IsFalse(cacher.IsCached("AA"));
            Assert.IsTrue(cacher.IsCached("BB"));
            Assert.IsTrue(cacher.IsCached("CC"));
            Assert.IsNotNull(listener2.Value);
            Assert.IsNotNull(listener3.Value);

            var value2 = listener2.Value;
            var value3 = listener3.Value;

            Assert.AreEqual("BB", value2.Key);
            Assert.AreEqual("CC", value3.Key);
            Assert.IsFalse(value2.IsDestroyed);
            Assert.IsFalse(value3.IsDestroyed);

            cacher.Remove(listener2);
            Assert.IsFalse(cacher.IsCached("BB"));
            Assert.IsTrue(value2.IsDestroyed);

            cacher.RemoveDelayed(listener3, 1f);
            Assert.IsTrue(cacher.IsCached("CC"));
            Assert.IsFalse(value3.IsDestroyed);

            yield return(new WaitForSeconds(0.5f));

            Assert.IsTrue(cacher.IsCached("CC"));
            Assert.IsFalse(value3.IsDestroyed);
            yield return(new WaitForSeconds(1.5f));

            Assert.IsFalse(cacher.IsCached("CC"));
            Assert.IsTrue(value3.IsDestroyed);
        }
Exemplo n.º 3
0
        public IEnumerator TestRequest()
        {
            var cacher    = new DummyCacher();
            var listener  = cacher.Request("Key1");
            var listener2 = cacher.Request("Key2");

            Assert.IsNull(listener.Value);
            Assert.IsNull(listener2.Value);
            Assert.IsFalse(cacher.IsCached("Key1"));
            Assert.IsFalse(cacher.IsCached("Key2"));

            // Premature checking
            yield return(new WaitForSecondsRealtime(0.5f));

            Assert.IsNull(listener.Value);
            Assert.IsNull(listener2.Value);
            Assert.IsFalse(cacher.IsCached("Key1"));
            Assert.IsFalse(cacher.IsCached("Key2"));

            // Guaranteed finished checking
            float limit = 3f;

            while (!cacher.IsCached("Key1") || !cacher.IsCached("Key2"))
            {
                limit -= Time.deltaTime;
                if (limit <= 0)
                {
                    Assert.Fail("Request should've finished by now.");
                }
                yield return(null);
            }
            Assert.AreEqual(1, listener.Progress);
            Assert.AreEqual(1, listener2.Progress);
            Assert.IsNotNull(listener.Value);
            Assert.IsNotNull(listener2.Value);
            Assert.IsTrue(cacher.IsCached("Key1"));
            Assert.IsTrue(cacher.IsCached("Key2"));
            Assert.IsFalse(cacher.IsCached("ASDF"));
            Assert.AreEqual("Key1", listener.Value.Key);
            Assert.AreEqual("Key2", listener2.Value.Key);
        }
Exemplo n.º 4
0
        public IEnumerator TestDataLockCached()
        {
            var cacher    = new DummyCacher();
            var listeners = new CacheListener <DummyCacherData> [3];

            for (int i = 0; i < listeners.Length; i++)
            {
                listeners[i] = cacher.Request("a");
            }

            yield return(new WaitForSeconds(1.5f));

            Assert.IsTrue(cacher.IsCached("a"));

            for (int i = 0; i < listeners.Length - 1; i++)
            {
                cacher.Remove(listeners[i]);
            }
            Assert.IsTrue(cacher.IsCached("a"));
            cacher.Remove(listeners[2]);
            Assert.IsFalse(cacher.IsCached("a"));
            Assert.IsTrue(listeners[listeners.Length - 1].Value.IsDestroyed);
        }
Exemplo n.º 5
0
        public IEnumerator TestRequest()
        {
            var cacher = new DummyCacher();
            var agent  = new CacherAgent <string, DummyCacherData>(cacher);

            Assert.AreEqual(cacher, agent.Cacher);

            agent.UseDelayedRemove = false;
            agent.RemoveDelay      = 0f;

            Assert.IsNull(agent.Listener);
            Assert.IsFalse(cacher.IsCached("A"));

            // Request data A.
            agent.Request("A");
            Assert.IsFalse(cacher.IsCached("A"));
            Assert.IsNotNull(agent.Listener);

            float limit = 2f;

            while (!cacher.IsCached("A"))
            {
                Debug.Log("Progress: " + agent.Listener.Progress);
                limit -= Time.deltaTime;
                if (limit <= 0)
                {
                    Assert.Fail("Request should've finished by now!");
                }
                yield return(null);
            }
            Assert.IsTrue(cacher.IsCached("A"));
            Assert.AreEqual(1f, agent.Listener.Progress);
            Assert.IsNotNull(agent.Listener.Value);
            Assert.AreEqual("A", agent.Listener.Key);
            Assert.AreEqual("A", agent.Listener.Value.Key);
            Assert.IsFalse(agent.Listener.Value.IsDestroyed);

            // Request data B.
            agent.Request("B");
            Assert.IsFalse(cacher.IsCached("B"));
            Assert.AreEqual(0f, agent.Listener.Progress);
            Assert.AreEqual("B", agent.Listener.Key);
            Assert.IsNull(agent.Listener.Value);

            limit = 2f;
            while (!cacher.IsCached("B"))
            {
                Debug.Log("Progress: " + agent.Listener.Progress);
                limit -= Time.deltaTime;
                if (limit <= 0)
                {
                    Assert.Fail("Request should've finished by now!");
                }
                yield return(null);
            }
            Assert.IsTrue(cacher.IsCached("B"));
            Assert.AreEqual(1f, agent.Listener.Progress);
            Assert.IsNotNull(agent.Listener.Value);
            Assert.AreEqual("B", agent.Listener.Key);
            Assert.AreEqual("B", agent.Listener.Value.Key);
            Assert.IsFalse(agent.Listener.Value.IsDestroyed);
        }