예제 #1
0
        public void Test(string token, IntervalType expectedIntervalType)
        {
            // Arrange
            IntervalParser intervalParser = new IntervalParser();

            // Act
            IntervalType actualIntervalType = intervalParser.Parse(token);

            // Assert
            Assert.Equal(expectedIntervalType, actualIntervalType);
        }
예제 #2
0
 public void CanParseIntervals(string str, TimeSpan?expected)
 {
     if (str == null)
     {
         Assert.Throws <ArgumentNullException>(() => IntervalParser.Parse(str));
     }
     else if (expected == null)
     {
         Assert.Throws <ArgumentException>(() => IntervalParser.Parse(str));
     }
     else
     {
         Assert.Equal(expected, IntervalParser.Parse(str));
     }
 }
예제 #3
0
        public void Parse_ValidParse_ResultEqualsExpected <TSet, TStruct, TParser> (
            String inputString,
            TSet hack1,
            TStruct hack2,
            TParser hack3,
            IntervalParser <TSet, TStruct, TParser> parser,
            Interval <TSet, TStruct> expected)
            where TSet : IComparable
            where TStruct : IStructure, new()
            where TParser : IParser <TSet>, new()
        {
            var result = parser.Parse(inputString);

            Assert.IsNotNull(result);

            Assert.AreEqual(expected, result);
        }
예제 #4
0
        public static IServiceCollection AddWhaleCache(this IServiceCollection services, ServiceConfig config, ILogger logger)
        {
            logger?.LogInformation($"Default object cache lifetime: {config.CacheTtl}");

            if (string.IsNullOrEmpty(config.RedisCache))
            {
                logger?.LogInformation("Using in-memory cache.");
                services.AddSingleton <IMemoryCache>(new MemoryCache(new MemoryCacheOptions {
                }));
                services.AddSingleton <ICacheFactory>(provider => new MemCacheFactory(provider.GetService <IMemoryCache>())
                {
                    Ttl = IntervalParser.Parse(config.CacheTtl)
                });
            }
            else
            {
                logger?.LogInformation($"Using Redis cache ({config.RedisCache})");
                var ready     = false;
                var retryTime = 15;
                while (!ready)
                {
                    try
                    {
                        services.AddSingleton <IConnectionMultiplexer>(ConnectionMultiplexer.Connect(config.RedisCache));
                        ready = true;
                    }
                    catch (Exception ex)
                    {
                        logger?.LogWarning($"Could not connect to redis instance. Retrying in {retryTime}s.");
                        logger?.LogInformation($"Redis connection string: {config.RedisCache}");
                        logger?.LogError(ex, "Redis connection error");
                        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(retryTime));
                    }
                }

                services.AddSingleton <ICacheFactory>(p => new RedCacheFactory {
                    Mux = p.GetService <IConnectionMultiplexer>(), Ttl = IntervalParser.Parse(config.CacheTtl)
                });
            }

            return(services);
        }
예제 #5
0
        private static MutableString/*!*/ TrInternal(MutableString/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ from,
            [DefaultProtocol, NotNull]MutableString/*!*/ to, bool squeeze) {

            MutableString result = self.CreateInstance().TaintBy(self);
            IntervalParser parser = new IntervalParser(from);

            // TODO: a single pass to generate both?
            MutableString source = parser.ParseSequence();
            BitArray bitmap = parser.Parse();

            MutableString dest = new IntervalParser(to).ParseSequence();

            int lastChar = dest.GetLastChar();
            char? lastTranslated = null;
            for (int i = 0; i < self.Length; i++) {
                char c = self.GetChar(i);
                if (bitmap.Get(c)) {
                    char? thisTranslated = null;
                    int index = source.IndexOf(c);
                    if (index >= dest.Length) {
                        if (lastChar != -1) {
                            thisTranslated = (char)lastChar;
                        }
                    } else {
                        thisTranslated = dest.GetChar(index);
                    }
                    if (thisTranslated != null && (!squeeze || lastTranslated == null || lastTranslated.Value != thisTranslated)) {
                        result.Append(thisTranslated.Value);
                    }
                    lastTranslated = thisTranslated;
                } else {
                    result.Append(c);
                    lastTranslated = null;
                }
            }

            return result;
        }