public async void OnStreamUpdate_Called_When_StreamAlreadyOnline() { var usersFollowsResponseJson = JMock.Of <GetStreamsResponse>(o => o.Streams == new[] { Mock.Of <Stream>(u => u.UserId == "UserId") } ); var mockHandler = TwitchLibMock.HttpCallHandler(("https://api.twitch.tv/helix/streams", usersFollowsResponseJson)); _api = TwitchLibMock.TwitchApi(mockHandler); var eventExecuteCount = 0; _liveStreamMonitor = new LiveStreamMonitorService(_api); _liveStreamMonitor.SetChannelsById(Utils.CreateListWithStrings("UserId")); _liveStreamMonitor.OnStreamUpdate += (sender, e) => eventExecuteCount++; await _liveStreamMonitor.UpdateLiveStreamersAsync(); await _liveStreamMonitor.UpdateLiveStreamersAsync(); Assert.Equal(1, eventExecuteCount); }
public async void LiveStreams_Contains_UserId_When_ServiceUpdated() { var usersFollowsResponseJson = JMock.Of <GetStreamsResponse>(o => o.Streams == new[] { Mock.Of <Stream>(u => u.UserId == "UserId") } ); _api = TwitchLibMock.TwitchApi( ("https://api.twitch.tv/helix/streams", usersFollowsResponseJson) ); _liveStreamMonitor = new LiveStreamMonitorService(_api); _liveStreamMonitor.SetChannelsById(Utils.CreateListWithStrings("UserId")); await _liveStreamMonitor.UpdateLiveStreamersAsync(); Assert.True(_liveStreamMonitor.LiveStreams.ContainsKey("UserId")); }
static TwitchUtility() { if (Ditto.Twitch == null) { return; } Links = new ConcurrentDictionary <int, Link>(); IsMonitoring = false; Ditto.Connected += () => { var _ = Task.Run(async() => { _cancellationTokenSource?.Cancel(); _cancellationTokenSource = new CancellationTokenSource(); await Ditto.Database.ReadAsync((uow) => { Links = new ConcurrentDictionary <int, Link>( uow.Links.GetAllWithLinks() .Where(e => e.Type == LinkType.Twitch) .Select(i => new KeyValuePair <int, Link>(i.Id, i)) ); }); try { if (Monitor == null) { Monitor = new LiveStreamMonitorService(Ditto.Twitch, 60); Monitor.OnStreamOnline += Monitor_OnStreamOnline; Monitor.OnStreamOffline += Monitor_OnStreamOffline; Monitor.OnStreamUpdate += Monitor_OnStreamUpdate; // Start monitoring the twitch links, this will notify users when a stream switches it's live status. var channels = Links.ToList().Select(e => e.Value.Value.Split("|", StringSplitOptions.RemoveEmptyEntries).FirstOrDefault()).ToList(); MonitorChannels(channels); // Instantly update the monitoring service on load. if (Links.Count > 0) { await Monitor.UpdateLiveStreamersAsync(true).ConfigureAwait(false); } } } catch (Exception ex) { Log.Error($"Twitch | {ex}"); } }); return(Task.CompletedTask); }; Ditto.Exit += () => { _cancellationTokenSource?.Cancel(); Links?.Clear(); return(Task.CompletedTask); }; }