public async Task Can_handle_request_session_finish_even_when_deposit_is_unknown() { ConsumerSession consumerSession = new ConsumerSession(_session1Id, _missingDepositId, _asset1Id, _consumerAddress, _consumerNodeId, _providerAddress, _providerNodeId, SessionState.Started, 1, 2, 4); await _sessionService.SendFinishSessionAsync(_missingDepositId); _ndmPeer.DidNotReceive().SendFinishSession(_deposit1Id); }
public async Task Can_add_async() { IMongoDatabase database = MongoForTest.TempDb.GetDatabase(); var repo = new ConsumerSessionMongoRepository(database); ConsumerSession session = BuildDummySession(); await repo.AddAsync(session); }
private ConsumerSession[] Filter( Keccak?depositId = null, Keccak?dataAssetId = null, PublicKey?consumerNodeId = null, Address?consumerAddress = null, PublicKey?providerNodeId = null, Address?providerAddress = null) { byte[][] sessionsBytes = _database.GetAllValues().ToArray(); if (sessionsBytes.Length == 0) { return(Array.Empty <ConsumerSession>()); } ConsumerSession[] sessions = new ConsumerSession[sessionsBytes.Length]; for (int i = 0; i < sessionsBytes.Length; i++) { sessions[i] = Decode(sessionsBytes[i]); } if (depositId is null && dataAssetId is null && consumerNodeId is null && consumerAddress is null && providerNodeId is null && providerAddress is null) { return(sessions); } IEnumerable <ConsumerSession> filteredSessions = sessions.AsEnumerable(); if (!(depositId is null)) { filteredSessions = filteredSessions.Where(s => s.DepositId == depositId); } if (!(dataAssetId is null)) { filteredSessions = filteredSessions.Where(s => s.DataAssetId == dataAssetId); } if (!(consumerNodeId is null)) { filteredSessions = filteredSessions.Where(s => s.ConsumerNodeId == consumerNodeId); } if (!(consumerAddress is null)) { filteredSessions = filteredSessions.Where(s => s.ConsumerAddress == consumerAddress); } if (!(providerNodeId is null)) { filteredSessions = filteredSessions.Where(s => s.ProviderNodeId == providerNodeId); } if (!(providerAddress is null)) { filteredSessions = filteredSessions.Where(s => s.ProviderAddress == providerAddress); } return(filteredSessions.OrderByDescending(s => s.StartTimestamp).ToArray()); }
public ConsumerSessionForRpc(ConsumerSession session) : base(session) { ConsumedUnitsFromProvider = session.ConsumedUnitsFromProvider; DataAvailability = session.DataAvailability.ToString().ToLowerInvariant(); StreamEnabled = session.StreamEnabled; Subscriptions = session.Subscriptions; }
private Task AddOrUpdateAsync(ConsumerSession session) { Serialization.Rlp.Rlp rlp = _rlpDecoder.Encode(session); _database.Set(session.Id, rlp.Bytes); return(Task.CompletedTask); }
public async Task Previous_of_non_persisted() { IDb db = new MemDb(); ConsumerSessionRocksRepository repository = new ConsumerSessionRocksRepository(db, new ConsumerSessionDecoder()); ConsumerSession retrieved = await repository.GetPreviousAsync(_cases[1]); retrieved.Should().BeNull(); }
private IReadOnlyList <ConsumerSession> Filter(Keccak depositId = null, Keccak dataAssetId = null, PublicKey consumerNodeId = null, Address consumerAddress = null, PublicKey providerNodeId = null, Address providerAddress = null) { var sessionsBytes = _database.GetAll(); if (sessionsBytes.Length == 0) { return(Array.Empty <ConsumerSession>()); } var sessions = new ConsumerSession[sessionsBytes.Length]; for (var i = 0; i < sessionsBytes.Length; i++) { sessions[i] = Decode(sessionsBytes[i]); } if (depositId is null && dataAssetId is null && consumerNodeId is null && consumerAddress is null && providerNodeId is null && providerAddress is null) { return(sessions); } var filteredSessions = sessions.AsEnumerable(); if (!(depositId is null)) { filteredSessions = filteredSessions.Where(s => s.DepositId == depositId); } if (!(dataAssetId is null)) { filteredSessions = filteredSessions.Where(s => s.DataAssetId == dataAssetId); } if (!(consumerNodeId is null)) { filteredSessions = filteredSessions.Where(s => s.ConsumerNodeId == consumerNodeId); } if (!(consumerAddress is null)) { filteredSessions = filteredSessions.Where(s => s.ConsumerAddress == consumerAddress); } if (!(providerNodeId is null)) { filteredSessions = filteredSessions.Where(s => s.ProviderNodeId == providerNodeId); } if (!(providerAddress is null)) { filteredSessions = filteredSessions.Where(s => s.ProviderAddress == providerAddress); } return(filteredSessions.OrderByDescending(s => s.StartTimestamp).ToArray()); }
public async Task Can_request_session_finish() { ConsumerSession consumerSession = new ConsumerSession(_session1Id, _deposit1Id, _asset1Id, _consumerAddress, _consumerNodeId, _providerAddress, _providerNodeId, SessionState.Started, 1, 2, 4); await _sessionService.StartSessionAsync(consumerSession, _ndmPeer); await _sessionService.SendFinishSessionAsync(_deposit1Id); _ndmPeer.Received().SendFinishSession(_deposit1Id); }
public async Task Cannot_start_session_for_a_null_provider() { ConsumerSession consumerSession = new ConsumerSession(_session1Id, _deposit1Id, _asset1Id, _consumerAddress, _consumerNodeId, null, _providerNodeId, SessionState.Started, 1, 2, 4); await _sessionService.StartSessionAsync(consumerSession, _ndmPeer); var result = _sessionService.GetActive(_missingDepositId); result.Should().BeNull(); }
public async Task Adds_upfront_units_to_session_on_start() { ConsumerSession consumerSession = new ConsumerSession(_session1Id, _deposit1Id, _asset1Id, _consumerAddress, _consumerNodeId, _providerAddress, _providerNodeId, SessionState.Started, 1, 2, 4); await _sessionService.StartSessionAsync(consumerSession, _ndmPeer); var result = _sessionService.GetActive(_deposit1Id); result.UnpaidUnits.Should().Be((uint)_asset1.Rules.UpfrontPayment.Value); }
public async Task Cannot_start_session_with_timestamp_before_confirmation_timestamp() { ConsumerSession consumerSession = new ConsumerSession(_session1Id, _deposit1Id, _asset1Id, _consumerAddress, _consumerNodeId, _providerAddress, _providerNodeId, SessionState.Started, 1, 2, 0); await _sessionService.StartSessionAsync(consumerSession, _ndmPeer); var result = _sessionService.GetActive(_deposit1Id); result.Should().BeNull(); }
public async Task Cannot_start_session_when_data_asset_ids_do_not_match_between_session_and_deposit() { ConsumerSession consumerSession = new ConsumerSession(_session1Id, _deposit1Id, _asset2Id, _consumerAddress, _consumerNodeId, _providerAddress, _providerNodeId, SessionState.Started, 1, 2, 4); await _sessionService.StartSessionAsync(consumerSession, _ndmPeer); var result = _sessionService.GetActive(_deposit1Id); result.Should().BeNull(); }
public async Task Cannot_start_session_for_an_unknown_data_asset() { ConsumerSession consumerSession = new ConsumerSession(_session1Id, _depositForMissingId, _missingAssetId, _consumerAddress, _consumerNodeId, _providerAddress, _providerNodeId, SessionState.Started, 1, 2, 4); await _sessionService.StartSessionAsync(consumerSession, _ndmPeer); var result = _sessionService.GetActive(_depositForMissingId); result.Should().BeNull(); }
public async Task Update_get(ConsumerSession session) { IDb db = new MemDb(); ConsumerSessionRocksRepository repository = new ConsumerSessionRocksRepository(db, new ConsumerSessionDecoder()); await repository.UpdateAsync(session); ConsumerSession retrieved = await repository.GetAsync(session.Id); retrieved.Should().BeEquivalentTo(session); }
public async Task Adds_unpaid_units_based_on_time_between_session_and_confirmation() { ConsumerSession consumerSession = new ConsumerSession(_session1Id, _deposit2Id, _asset2Id, _consumerAddress, _consumerNodeId, _providerAddress, _providerNodeId, SessionState.Started, 1, 2, 4); await _sessionService.StartSessionAsync(consumerSession, _ndmPeer); var result = _sessionService.GetActive(_deposit2Id); result.Should().NotBeNull(); result.UnpaidUnits.Should().Be((uint)(consumerSession.StartTimestamp - _details2.ConfirmationTimestamp)); }
public async Task Can_get_by_id() { IMongoDatabase database = MongoForTest.TempDb.GetDatabase(); var repo = new ConsumerSessionMongoRepository(database); ConsumerSession session = BuildDummySession(); await repo.AddAsync(session); var result = await repo.GetAsync(session.Id); result.Should().BeEquivalentTo(session); }
public async Task Can_get_previous_returns_null_if_only_same() { IMongoDatabase database = MongoForTest.TempDb.GetDatabase(); var repo = new ConsumerSessionMongoRepository(database); ConsumerSession session = BuildDummySession(); await repo.AddAsync(session); var result = await repo.GetPreviousAsync(session); result.Should().BeNull(); }
public async Task Can_finish_provider_sessions_and_remove_provider() { ConsumerSession consumerSession = new ConsumerSession(_session1Id, _deposit1Id, _asset1Id, _consumerAddress, _consumerNodeId, _providerAddress, _providerNodeId, SessionState.Started, 1, 2, 4); await _sessionService.StartSessionAsync(consumerSession, _ndmPeer); await _sessionService.FinishSessionsAsync(_ndmPeer, true); var result = _sessionService.GetActive(_deposit1Id); result.Should().BeNull(); _providerService.GetPeer(_ndmPeer.ProviderAddress).Should().BeNull(); }
public async Task Can_retrieve_all_active_sessions_when_one_expired() { ConsumerSession consumerSession1 = new ConsumerSession(_session1Id, _deposit1Id, _asset1Id, _consumerAddress, _consumerNodeId, _providerAddress, _providerNodeId, SessionState.Started, 1, 2, 4); ConsumerSession consumerSession2 = new ConsumerSession(_session2Id, _deposit1Id, _asset1Id, _consumerAddress, _consumerNodeId, _providerAddress, _providerNodeId, SessionState.Started, 1, 2, 4); await _sessionService.StartSessionAsync(consumerSession1, _ndmPeer); await _sessionService.StartSessionAsync(consumerSession2, _ndmPeer); var result = _sessionService.GetAllActive(); result.Should().HaveCount(1); }
public async Task Can_handle_request_session_finish_even_when_peer_is_missing() { ConsumerSession consumerSession = new ConsumerSession(_session1Id, _deposit1Id, _asset1Id, _consumerAddress, _consumerNodeId, TestItem.AddressD, _providerNodeId, SessionState.Started, 1, 2, 4); INdmPeer peer = Substitute.For <INdmPeer>(); peer.ProviderAddress.Returns(TestItem.AddressD); await _sessionService.StartSessionAsync(consumerSession, peer); await _sessionService.SendFinishSessionAsync(_deposit1Id); _ndmPeer.DidNotReceive().SendFinishSession(_deposit1Id); }
public async Task Can_get_previous() { IMongoDatabase database = MongoForTest.TempDb.GetDatabase(); var repo = new ConsumerSessionMongoRepository(database); ConsumerSession session = BuildDummySession(); await repo.AddAsync(session); ConsumerSession session2 = BuildDummySession(Keccak.Zero); var result = await repo.GetPreviousAsync(session2); result.Should().BeEquivalentTo(session); }
public async Task Previous_of_second() { IDb db = new MemDb(); ConsumerSessionRocksRepository repository = new ConsumerSessionRocksRepository(db, new ConsumerSessionDecoder()); await repository.AddAsync(_cases[1]); await repository.AddAsync(_cases[0]); ConsumerSession retrieved = await repository.GetPreviousAsync(_cases[1]); retrieved.Should().BeEquivalentTo(_cases[0]); }
public async Task Cannot_start_session_for_an_unknown_provider() { ConsumerSession consumerSession = new ConsumerSession(_session1Id, _deposit1Id, _asset1Id, _consumerAddress, _consumerNodeId, _providerAddress, _providerNodeId, SessionState.Started, 1, 2, 4); INdmPeer mismatchedPeer = Substitute.For <INdmPeer>(); mismatchedPeer.ProviderAddress.Returns(TestItem.AddressD); mismatchedPeer.NodeId.Returns(TestItem.PublicKeyD); await _sessionService.StartSessionAsync(consumerSession, mismatchedPeer); var result = _sessionService.GetActive(_missingDepositId); result.Should().BeNull(); }
public async Task Will_not_finish_sessions_from_other_providers() { ConsumerSession consumerSession = new ConsumerSession(_session1Id, _deposit1Id, _asset1Id, _consumerAddress, _consumerNodeId, _providerAddress, _providerNodeId, SessionState.Started, 1, 2, 4); await _sessionService.StartSessionAsync(consumerSession, _ndmPeer); INdmPeer otherPeer = Substitute.For <INdmPeer>(); otherPeer.ProviderAddress.Returns(TestItem.AddressD); otherPeer.NodeId.Returns(TestItem.PublicKeyD); await _sessionService.FinishSessionsAsync(otherPeer, true); var result = _sessionService.GetActive(_deposit1Id); result.Should().NotBeNull(); _providerService.GetPeer(_ndmPeer.ProviderAddress).Should().NotBeNull(); }
public async Task <ConsumerSession> GetPreviousAsync(ConsumerSession session) { var previousSessions = await Filter(session.DepositId).Take(2).ToListAsync(); switch (previousSessions.Count) { case 0: return(null); case 1: return(GetUniqueSession(session, previousSessions[0])); default: { return(GetUniqueSession(session, previousSessions[1]) ?? GetUniqueSession(session, previousSessions[0])); } } }
public async Task Can_browse_with_query_and_pagination() { IMongoDatabase database = MongoForTest.TempDb.GetDatabase(); var repo = new ConsumerSessionMongoRepository(database); ConsumerSession session = BuildDummySession(); await repo.AddAsync(session); var query = new GetConsumerSessions(); query.ConsumerAddress = session.ConsumerAddress; query.DepositId = session.DepositId; query.ConsumerAddress = session.ConsumerAddress; query.ConsumerNodeId = session.ConsumerNodeId; query.DataAssetId = session.DataAssetId; query.ConsumerNodeId = session.ConsumerNodeId; query.Page = 0; query.Results = 10; await repo.BrowseAsync(query); }
public Task <ConsumerSession?> GetPreviousAsync(ConsumerSession session) { ConsumerSession[] sessions = Filter(session.DepositId); switch (sessions.Length) { case 0: return(Task.FromResult <ConsumerSession?>(null)); case 1: return(Task.FromResult <ConsumerSession?>(GetUniqueSession(session, sessions[0]))); default: { ConsumerSession[] previousSessions = sessions.Take(2).ToArray(); return(Task.FromResult <ConsumerSession?>(GetUniqueSession(session, previousSessions[1]) ?? GetUniqueSession(session, previousSessions[0]))); } } }
private static ConsumerSession BuildDummySession(Keccak id) { ConsumerSession session = new ConsumerSession( id, TestItem.KeccakB, TestItem.KeccakC, TestItem.AddressA, TestItem.PublicKeyA, TestItem.AddressB, TestItem.PublicKeyB, SessionState.ConsumerDisconnected, 1, 2, 3, 4, 5, 6, 7, 8, 9, DataAvailability.SubscriptionEnded); return(session); }
public ConsumerSessionForRpc(ConsumerSession session) : base(session) { ConsumedUnitsFromProvider = session.ConsumedUnitsFromProvider; DataAvailability = session.DataAvailability.ToString().ToLowerInvariant(); Clients = session.Clients.Select(c => new SessionClientForRpc(c)).ToArray(); }
private static ConsumerSession GetUniqueSession(ConsumerSession current, ConsumerSession previous) => current.Equals(previous) ? null : previous;