Exemplo n.º 1
0
        public void This_Client_Always_Contains_Active_Current_ClientId()
        {
            var clientUuid   = Guid.NewGuid().ToString();
            var clientRecord = new ClientRecordsIndex()
            {
                Clients = new Dictionary <string, ClientRecordEntry>()
            };

            var parsedHlc = new ParsedHLC("0", "real");

            var clientRecordDetails = new ClientRecordDetails(clientRecord, parsedHlc, clientUuid, TimeSpan.FromSeconds(1));

            Assert.Equal(0, clientRecordDetails.NumExpiredClients);
            Assert.Equal(0, clientRecordDetails.ExpiredClientIds.Count);
            Assert.Contains(clientUuid, clientRecordDetails.SortedActiveClientIds);
            Assert.Equal(0, clientRecordDetails.IndexOfThisClient);
        }
Exemplo n.º 2
0
        public void This_Client_Does_Not_Count_As_Expired()
        {
            var clientUuid   = Guid.NewGuid().ToString();
            var clientRecord = new ClientRecordsIndex()
            {
                Clients = new Dictionary <string, ClientRecordEntry>()
                {
                    // Other, expired client.
                    { Guid.NewGuid().ToString(),
                      new ClientRecordEntry()
                      {
                          ExpiresMilliseconds  = 10,
                          HeartbeatMutationCas = "0x0000b12a92016516",
                          NumAtrs = 1024
                      } },

                    // This client, also expired.
                    { clientUuid,
                      new ClientRecordEntry()
                      {
                          ExpiresMilliseconds  = 10,
                          HeartbeatMutationCas = "0x0000b12a92016516",
                          NumAtrs = 1024
                      } }
                }
            };

            var parsedHlc = new ParsedHLC(DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(System.Globalization.CultureInfo.InvariantCulture), "real");

            var clientRecordDetails = new ClientRecordDetails(clientRecord, parsedHlc, clientUuid, TimeSpan.FromSeconds(1));

            _outputHelper.WriteLine(JObject.FromObject(clientRecordDetails).ToString());
            Assert.Equal(2, clientRecordDetails.NumExistingClients);
            Assert.Equal(1, clientRecordDetails.NumExpiredClients);
            Assert.Equal(1, clientRecordDetails.ExpiredClientIds.Count);
            Assert.Contains(clientUuid, clientRecordDetails.SortedActiveClientIds);
            Assert.NotEqual(-1, clientRecordDetails.IndexOfThisClient);
        }
        public ClientRecordDetails(ClientRecordsIndex clientRecord, ParsedHLC parsedHlc, string clientUuid, TimeSpan cleanupWindow)
        {
            _               = clientRecord ?? throw new ArgumentNullException(nameof(clientRecord));
            _               = parsedHlc ?? throw new ArgumentNullException(nameof(parsedHlc));
            CasNowNanos     = parsedHlc.NowTime.ToUnixTimeMilliseconds() * NanosecondsPerMillisecond;
            OverrideEnabled = clientRecord.Override?.Enabled == true;
            OverrideExpires = clientRecord.Override?.Expires;
            OverrideActive  = OverrideEnabled && parsedHlc.NowTime < OverrideExpires;
            var  clientCount             = clientRecord.Clients?.Count ?? 0;
            var  expiredClientIds        = new List <string>(clientCount);
            var  activeClientIds         = new List <string>(clientCount);
            bool thisClientAlreadyExists = false;

            if (clientCount > 0)
            {
                foreach (var kvp in clientRecord.Clients !)
                {
                    var uuid         = kvp.Key;
                    var client       = kvp.Value;
                    var parsedHlcNow = parsedHlc.NowTime;

                    // (Note, do not include this client as expired, as it is about to add itself)
                    if (uuid == clientUuid)
                    {
                        activeClientIds.Add(uuid);
                        thisClientAlreadyExists = true;
                    }
                    else if (client.ParsedMutationCas == null || client.Expires < parsedHlcNow)
                    {
                        expiredClientIds.Add(uuid);
                    }
                    else
                    {
                        activeClientIds.Add(uuid);
                    }
                }
            }

            if (!thisClientAlreadyExists)
            {
                activeClientIds.Add(clientUuid);
            }

            var sortedActiveClientIds = activeClientIds.ToList();

            sortedActiveClientIds.Sort();
            SortedActiveClientIds = sortedActiveClientIds;
            for (int i = 0; i < SortedActiveClientIds.Count; i++)
            {
                if (SortedActiveClientIds[i] == clientUuid)
                {
                    IndexOfThisClient = i;
                    break;
                }
            }

            ExpiredClientIds        = expiredClientIds.ToList();
            ActiveClientIds         = activeClientIds.ToList();
            AtrsHandledByThisClient = GetAtrsHandledByThisClient().ToList();
            var handledCount = AtrsHandledByThisClient.Count;

            handledCount       = handledCount == 0 ? 1 : handledCount;
            CheckAtrTimeWindow = cleanupWindow / handledCount;
        }