public async Task OpenCensusClientCache_HandlesAddOverflow()
        {
            var cache = new OpenCensusClientCache <string, Node>(10, TimeSpan.FromSeconds(1));

            var nodes = Enumerable.Range(0, 10).Select(i => (i.ToString(), CreateNode((uint)i))).ToArray();

            foreach (var node in nodes)
            {
                cache.AddOrUpdate(node.Item1, node.Item2);
            }


            await Task.Delay(2000);

            var newNode = CreateNode(42);

            cache.AddOrUpdate("new", newNode);

            foreach (var node in nodes)
            {
                Assert.IsFalse(cache.TryGet(node.Item1, out _));
            }

            Assert.IsTrue(cache.TryGet("new", out var actualNewNode));
            Assert.AreEqual(newNode, actualNewNode);
        }
        public async Task OpenCensusClientCache_TryGetUpdatesTimestamp()
        {
            var cache = new OpenCensusClientCache <string, Node>(10, TimeSpan.FromSeconds(1));

            var nodes = Enumerable.Range(0, 10).Select(i => (i.ToString(), CreateNode((uint)i))).ToArray();

            foreach (var node in nodes)
            {
                cache.AddOrUpdate(node.Item1, node.Item2);
            }


            await Task.Delay(2000);

            Assert.IsTrue(cache.TryGet(nodes[0].Item1, out _));

            cache.AddOrUpdate("new", CreateNode(42));

            for (int i = 1; i < nodes.Length; i++)
            {
                // spin a bit to avoid flakiness
                Assert.IsFalse(cache.TryGet(nodes[i].Item1, out _));
            }
            Assert.IsTrue(cache.TryGet("new", out _));
        }
        public void OpenCensusClientCache_GetNotExisting()
        {
            var cache = new OpenCensusClientCache <string, Node>();

            Assert.IsFalse(cache.TryGet("client0", out var node));
            Assert.IsNull(node);
        }
        public void OpenCensusClientCache_AddsAndUpdates()
        {
            var cache = new OpenCensusClientCache <string, Node>();

            var node0 = CreateNode(0);
            var node1 = CreateNode(1);

            cache.AddOrUpdate("client0", node0);
            cache.AddOrUpdate("client1", node1);
            cache.AddOrUpdate("client0", node1);

            Assert.IsTrue(cache.TryGet("client0", out var actualNode0));
            Assert.AreEqual(node1, actualNode0);

            Assert.IsTrue(cache.TryGet("client1", out var actualNode1));
            Assert.AreEqual(node1, actualNode1);
        }
예제 #5
0
        /// <summary>
        /// Gets or updates opencensus peer info.
        /// </summary>
        /// <param name="originalNode">Node in the message (or null).</param>
        /// <param name="callContext">Call context.</param>
        /// <param name="peerInfo">Cached peer info (or the new one).</param>
        /// <returns>True is peer info was found/avaialble, false otherwise.</returns>
        private bool TryGetOrUpdatePeerInfo(Node originalNode, ServerCallContext callContext, out Node peerInfo)
        {
            if (originalNode != null)
            {
                this.telemetryClient.TrackNodeEvent(originalNode, callContext.Method, callContext.Peer, this.ocToAiInstrumentationKey);
                peerInfo = opencensusPeers.AddOrUpdate(callContext.Peer, originalNode);
                return(true);
            }

            return(opencensusPeers.TryGet(callContext.Peer, out peerInfo));
        }
        public async Task OpenCensusClientCache_DoesNotRemoveItemsBeforeOverflow()
        {
            var cache = new OpenCensusClientCache <string, Node>(10, TimeSpan.FromSeconds(1));

            var nodes = Enumerable.Range(0, 10).Select(i => (i.ToString(), CreateNode((uint)i))).ToArray();

            foreach (var node in nodes)
            {
                cache.AddOrUpdate(node.Item1, node.Item2);
            }

            await Task.Delay(2000);

            foreach (var node in nodes)
            {
                Assert.IsTrue(cache.TryGet(node.Item1, out _));
            }
        }