public void Collection_is_thread_safe()
        {
            const int numberOfThreads = 500;
            var       threadAction    = new ThreadStart(
                () => {
                var extraSession1 = new InProcSession(new SessionId(Guid.NewGuid(), false), A.Dummy <ISession>(), _nowUtc.AddMinutes(-20), TimeSpan.FromMinutes(15));
                var extraSession2 = new InProcSession(new SessionId(Guid.NewGuid(), false), A.Dummy <ISession>(), _nowUtc, TimeSpan.FromMinutes(15));
                _inProcSessionCache.Set(extraSession1);
                Thread.Sleep(20);
                _inProcSessionCache.Get(extraSession1.Id);
                _inProcSessionCache.Set(extraSession2);
                _inProcSessionCache.Get(extraSession2.Id);
            });

            var threads = new List <Thread>();

            for (var i = 0; i < numberOfThreads; i++)
            {
                var newThread = new Thread(threadAction);
                newThread.Start();
                threads.Add(newThread);
            }
            threads.ForEach(thread => thread.Join());

            var expectedNumberOfSessions = numberOfThreads + _numberOfSessions;
            var actualNumberOfSessions   = _inProcSessionCache.Count;

            Assert.Equal(expectedNumberOfSessions, actualNumberOfSessions);
        }
        public void When_disposed_then_cannot_access_Set()
        {
            var extraSession = new InProcSession(new SessionId(Guid.NewGuid(), false), A.Dummy <ISession>(), _nowUtc, TimeSpan.FromMinutes(15));

            _inProcSessionCache.Dispose();
            Assert.Throws <ObjectDisposedException>(() => _inProcSessionCache.Set(extraSession));
        }
    public void Set_adds_new_element() {
      var extraSession = new InProcSession(new SessionId(Guid.NewGuid(), false), A.Dummy<ISession>(), _nowUtc, TimeSpan.FromMinutes(15));
      _inProcSessionCache.Set(extraSession);

      var expected = _numberOfSessions + 1;
      var actual = _inProcSessionCache.Count;
      Assert.Equal(expected, actual);
    }
    public void Set_replaces_item_when_element_already_is_cached() {
      var newSession = new InProcSession(_activeSession.Id, A.Dummy<ISession>(), _nowUtc.AddSeconds(10), TimeSpan.FromMinutes(10));
      _inProcSessionCache.Set(newSession);

      var actual = _inProcSessionCache.Count;
      Assert.Equal(_numberOfSessions, actual);

      var sessionAfterSave = _inProcSessionCache.Get(_activeSession.Id);
      Assert.Equal(sessionAfterSave.LastSave, newSession.LastSave);
    }
        public void Set_adds_new_element()
        {
            var extraSession = new InProcSession(new SessionId(Guid.NewGuid(), false), A.Dummy <ISession>(), _nowUtc, TimeSpan.FromMinutes(15));

            _inProcSessionCache.Set(extraSession);

            var expected = _numberOfSessions + 1;
            var actual   = _inProcSessionCache.Count;

            Assert.Equal(expected, actual);
        }
    public InProcSessionCacheFixture() {
      _nowUtc = new DateTime(2015, 10, 20, 21, 36, 14, DateTimeKind.Utc);
      _fakeSystemClock = A.Fake<ISystemClock>();
      ConfigureSystemClock_ToReturn(_nowUtc);

      _inProcSessionCache = new InProcSessionCache(_fakeSystemClock);
      _expiredSession = new InProcSession(new SessionId(Guid.NewGuid(), false), A.Dummy<ISession>(), _nowUtc.AddMinutes(-20), TimeSpan.FromMinutes(15));
      _activeSession = new InProcSession(new SessionId(Guid.NewGuid(), false), A.Dummy<ISession>(), _nowUtc.AddMinutes(-3), TimeSpan.FromMinutes(15));
      _inProcSessionCache.Set(_expiredSession);
      _inProcSessionCache.Set(_activeSession);
      _numberOfSessions = 2;
    }
        public InProcSessionCacheFixture()
        {
            _nowUtc          = new DateTime(2015, 10, 20, 21, 36, 14, DateTimeKind.Utc);
            _fakeSystemClock = A.Fake <ISystemClock>();
            ConfigureSystemClock_ToReturn(_nowUtc);

            _inProcSessionCache = new InProcSessionCache(_fakeSystemClock);
            _expiredSession     = new InProcSession(new SessionId(Guid.NewGuid(), false), A.Dummy <ISession>(), _nowUtc.AddMinutes(-20), TimeSpan.FromMinutes(15));
            _activeSession      = new InProcSession(new SessionId(Guid.NewGuid(), false), A.Dummy <ISession>(), _nowUtc.AddMinutes(-3), TimeSpan.FromMinutes(15));
            _inProcSessionCache.Set(_expiredSession);
            _inProcSessionCache.Set(_activeSession);
            _numberOfSessions = 2;
        }
    public void Set(InProcSession session) {
      if (session == null) throw new ArgumentNullException("session");
      CheckDisposed();

      using (new HeldWriteLock(_rwlock)) {
        var index = _sessions.IndexOf(session);

        if (index < 0) {
          _sessions.Add(session);
        } else {
          _sessions[index] = session;
        }
      }
    }
        public void Set_replaces_item_when_element_already_is_cached()
        {
            var newSession = new InProcSession(_activeSession.Id, A.Dummy <ISession>(), _nowUtc.AddSeconds(10), TimeSpan.FromMinutes(10));

            _inProcSessionCache.Set(newSession);

            var actual = _inProcSessionCache.Count;

            Assert.Equal(_numberOfSessions, actual);

            var sessionAfterSave = _inProcSessionCache.Get(_activeSession.Id);

            Assert.Equal(sessionAfterSave.LastSave, newSession.LastSave);
        }
Esempio n. 10
0
        public void Set(InProcSession session)
        {
            if (session == null)
            {
                throw new ArgumentNullException("session");
            }
            CheckDisposed();

            using (new HeldWriteLock(_rwlock)) {
                var index = _sessions.IndexOf(session);

                if (index < 0)
                {
                    _sessions.Add(session);
                }
                else
                {
                    _sessions[index] = session;
                }
            }
        }
    public void Collection_is_thread_safe() {
      const int numberOfThreads = 500;
      var threadAction = new ThreadStart(
        () => {
          var extraSession1 = new InProcSession(new SessionId(Guid.NewGuid(), false), A.Dummy<ISession>(), _nowUtc.AddMinutes(-20), TimeSpan.FromMinutes(15));
          var extraSession2 = new InProcSession(new SessionId(Guid.NewGuid(), false), A.Dummy<ISession>(), _nowUtc, TimeSpan.FromMinutes(15));
          _inProcSessionCache.Set(extraSession1);
          Thread.Sleep(20);
          _inProcSessionCache.Get(extraSession1.Id);
          _inProcSessionCache.Set(extraSession2);
          _inProcSessionCache.Get(extraSession2.Id);
        });

      var threads = new List<Thread>();
      for (var i = 0; i < numberOfThreads; i++) {
        var newThread = new Thread(threadAction);
        newThread.Start();
        threads.Add(newThread);
      }
      threads.ForEach(thread => thread.Join());

      var expectedNumberOfSessions = numberOfThreads + _numberOfSessions;
      var actualNumberOfSessions = _inProcSessionCache.Count;

      Assert.Equal(expectedNumberOfSessions, actualNumberOfSessions);
    }
 public void When_disposed_then_cannot_access_Set() {
   var extraSession = new InProcSession(new SessionId(Guid.NewGuid(), false), A.Dummy<ISession>(), _nowUtc, TimeSpan.FromMinutes(15));
   _inProcSessionCache.Dispose();
   Assert.Throws<ObjectDisposedException>(() => _inProcSessionCache.Set(extraSession));
 }