Beispiel #1
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            string lockId = SingletonManager.FormatLockId(_method, _attribute.Scope);

            lockId += ".Listener";

            // for listener locks, if the user hasn't explicitly set an override on the
            // attribute, we want to default the timeout to the lock period. We want to
            // stop as soon as possible (since we want startup to be relatively fast)
            // however we can't give up before waiting for a natural lease expiry.
            if (_attribute.LockAcquisitionTimeout == null)
            {
                _attribute.LockAcquisitionTimeout = (int)_singletonManager.Config.LockPeriod.TotalSeconds;
            }

            _lockHandle = await _singletonManager.TryLockAsync(lockId, null, _attribute, cancellationToken);

            if (_lockHandle == null)
            {
                // if we're unable to acquire the lock, it means another listener
                // has it so we return w/o starting our listener
                return;
            }

            await _innerListener.StartAsync(cancellationToken);

            _isListening = true;
        }
        public void FormatLockId_ReturnsExpectedValue(string scope, string expectedLockId)
        {
            MethodInfo methodInfo   = this.GetType().GetMethod("TestJob", BindingFlags.Static | BindingFlags.NonPublic);
            string     actualLockId = SingletonManager.FormatLockId(methodInfo, scope);

            Assert.Equal(expectedLockId, actualLockId);
        }
Beispiel #3
0
 public void FormatLockId_ReturnsExpectedValue(SingletonScope scope, string scopeId, string expectedLockId)
 {
     MethodInfo methodInfo = this.GetType().GetMethod("TestJob", BindingFlags.Static | BindingFlags.NonPublic);
     var descriptor = FunctionIndexer.FromMethod(methodInfo);
     string actualLockId = SingletonManager.FormatLockId(descriptor, scope, "TestHostId", scopeId);
     Assert.Equal(expectedLockId, actualLockId);
 }
Beispiel #4
0
 public SingletonValueProviderTests()
 {
     _attribute = new SingletonAttribute("TestScope");
     SingletonManager singletonManager = new SingletonManager();
     _method = GetType().GetMethod("TestJob", BindingFlags.Static | BindingFlags.Public);
     _lockId = SingletonManager.FormatLockId(_method, _attribute.Scope);
     _valueProvider = new SingletonValueProvider(_method, "TestScope", TestInstanceId, _attribute, singletonManager);
 }
Beispiel #5
0
        public SingletonValueProviderTests()
        {
            _attribute = new SingletonAttribute("TestScope");
            SingletonManager singletonManager = new SingletonManager(null, null, null, null, new FixedHostIdProvider(TestHostId));

            _method        = GetType().GetMethod("TestJob", BindingFlags.Static | BindingFlags.Public);
            _lockId        = SingletonManager.FormatLockId(_method, SingletonScope.Function, TestHostId, _attribute.ScopeId);
            _valueProvider = new SingletonValueProvider(_method, "TestScope", TestInstanceId, _attribute, singletonManager);
        }
Beispiel #6
0
        public SingletonListener(MethodInfo method, SingletonAttribute attribute, SingletonManager singletonManager, IListener innerListener)
        {
            _attribute = attribute;
            _singletonManager = singletonManager;
            _singletonConfig = _singletonManager.Config;
            _innerListener = innerListener;

            string boundScopeId = _singletonManager.GetBoundScopeId(_attribute.ScopeId);
            _lockId = singletonManager.FormatLockId(method, _attribute.Scope, boundScopeId);
            _lockId += ".Listener";
        }
Beispiel #7
0
        public SingletonListener(MethodInfo method, SingletonAttribute attribute, SingletonManager singletonManager, IListener innerListener)
        {
            _attribute        = attribute;
            _singletonManager = singletonManager;
            _singletonConfig  = _singletonManager.Config;
            _innerListener    = innerListener;

            string boundScopeId = _singletonManager.GetBoundScopeId(_attribute.ScopeId);

            _lockId  = singletonManager.FormatLockId(method, _attribute.Scope, boundScopeId);
            _lockId += ".Listener";
        }
        public SingletonListener(FunctionDescriptor method, SingletonAttribute attribute, SingletonManager singletonManager, IListener innerListener, ILoggerFactory loggerFactory)
        {
            _attribute        = attribute;
            _singletonManager = singletonManager;
            _singletonConfig  = _singletonManager.Options;
            _innerListener    = innerListener;

            string boundScopeId = _singletonManager.GetBoundScopeId(_attribute.ScopeId);

            _lockId  = singletonManager.FormatLockId(method, _attribute.Scope, boundScopeId);
            _lockId += ".Listener";

            _logger = loggerFactory?.CreateLogger(LogCategories.Singleton);
        }
        public SingletonListenerTests()
        {
            MethodInfo methodInfo = this.GetType().GetMethod("TestJob", BindingFlags.Static | BindingFlags.NonPublic);

            _attribute = new SingletonAttribute();
            _config    = new SingletonConfiguration
            {
                LockPeriod = TimeSpan.FromSeconds(20)
            };
            _mockSingletonManager = new Mock <SingletonManager>(MockBehavior.Strict);
            _mockSingletonManager.SetupGet(p => p.Config).Returns(_config);
            _mockInnerListener = new Mock <IListener>(MockBehavior.Strict);
            _listener          = new SingletonListener(methodInfo, _attribute, _mockSingletonManager.Object, _mockInnerListener.Object);
            _lockId            = SingletonManager.FormatLockId(methodInfo, _attribute.Scope) + ".Listener";
        }
Beispiel #10
0
        public SingletonListener(MethodInfo method, SingletonAttribute attribute, SingletonManager singletonManager, IListener innerListener, TraceWriter trace, ILoggerFactory loggerFactory)
        {
            _attribute        = attribute;
            _singletonManager = singletonManager;
            _singletonConfig  = _singletonManager.Config;
            _innerListener    = innerListener;

            string boundScopeId = _singletonManager.GetBoundScopeId(_attribute.ScopeId);

            _lockId  = singletonManager.FormatLockId(method, _attribute.Scope, boundScopeId);
            _lockId += ".Listener";

            _trace  = trace;
            _logger = loggerFactory?.CreateLogger(LogCategories.Singleton);
        }
Beispiel #11
0
        public SingletonListenerTests()
        {
            MethodInfo methodInfo = this.GetType().GetMethod("TestJob", BindingFlags.Static | BindingFlags.NonPublic);

            _attribute = new SingletonAttribute();
            _config    = new SingletonConfiguration
            {
                LockPeriod = TimeSpan.FromSeconds(20)
            };
            _mockSingletonManager = new Mock <SingletonManager>(MockBehavior.Strict, null, null, null, null, new FixedHostIdProvider(TestHostId), null);
            _mockSingletonManager.SetupGet(p => p.Config).Returns(_config);
            _mockInnerListener = new Mock <IListener>(MockBehavior.Strict);
            _listener          = new SingletonListener(methodInfo, _attribute, _mockSingletonManager.Object, _mockInnerListener.Object, new TestTraceWriter(System.Diagnostics.TraceLevel.Verbose));
            _lockId            = SingletonManager.FormatLockId(methodInfo, SingletonScope.Function, TestHostId, _attribute.ScopeId) + ".Listener";
        }
        public SingletonListenerTests()
        {
            MethodInfo methodInfo = this.GetType().GetMethod("TestJob", BindingFlags.Static | BindingFlags.NonPublic);
            var        descriptor = FunctionIndexer.FromMethod(methodInfo);

            _attribute = new SingletonAttribute();
            _config    = new SingletonConfiguration
            {
                LockPeriod = TimeSpan.FromSeconds(20)
            };
            _mockSingletonManager = new Mock <SingletonManager>(MockBehavior.Strict, null, null, null, null, new FixedHostIdProvider(testHostId), null);
            _mockSingletonManager.SetupGet(p => p.Config).Returns(_config);
            _mockInnerListener = new Mock <IListener>(MockBehavior.Strict);

            _listener = new SingletonListener(descriptor, _attribute, _mockSingletonManager.Object, _mockInnerListener.Object, null);
            _lockId   = SingletonManager.FormatLockId(descriptor, SingletonScope.Function, testHostId, _attribute.ScopeId) + ".Listener";
        }