public void should_throw_new_entries_if_queue_is_full()
        {
            var failed = false;
            var sut    = new UploadQueue <string>(x => Task.Delay(1000).Wait())
            {
                MaxQueueSize = 1,
                ActivateSync = true
            };

            sut.Enqueue("hello");

            sut.UploadFailed += (o, e) => failed = true;
            sut.Enqueue("hello");

            failed.Should().BeTrue();
        }
        private void Tallenna_Click(object sender, RoutedEventArgs e)
        {
            var tournament = (TrackableTournament)TournamentList.SelectedItem;

            if (tournament.Tracking && tournament.Name != tournament.Tournament.Name)
            {
                UploadEvent ev = new UploadEvent(() => _uploader.UploadName(tournament.Tournament.SanctionNumber, tournament.Name),
                                                 tournament.Tournament.WithName(tournament.Name), UploadEvent.Type.Name, 0);
                UploadQueue.Enqueue(ev);
                tournament.Tournament = tournament.Tournament.WithName(tournament.Name);
            }
        }
        public void should_invoke_Background_job()
        {
            var uploadIsInvoked = false;
            var sut             = new UploadQueue <string>(x => uploadIsInvoked = true)
            {
                ActivateSync = true
            };

            sut.Enqueue("hello");

            sut.Wait(100);
            sut.TaskWasInvoked.Should().BeTrue();
            uploadIsInvoked.Should().BeTrue();
        }
        public void should_not_invoke_Failed_report_if_upload_is_successful()
        {
            var sut = new UploadQueue <string>(x => { })
            {
                ActivateSync  = true,
                RetryInterval = TimeSpan.FromMilliseconds(10),
                MaxAttempts   = 3
            };
            var failed = false;

            sut.UploadFailed += (sender, args) => failed = true;

            sut.Enqueue("hello");

            sut.Wait(1000);
            failed.Should().BeFalse();
        }
        private void Poista_Click(object sender, RoutedEventArgs e)
        {
            var tournament = (TrackableTournament)TournamentList.SelectedItem;
            var confirm    = MessageBox.Show(
                "Haluatko varmasti poistaa turnauksen " + tournament.Tournament.SanctionNumber + " " + tournament.Name + "?",
                "Oletko varma?",
                MessageBoxButton.YesNo);

            if (confirm == MessageBoxResult.Yes)
            {
                tournament.Tracking           = false;
                tournament.TournamentUploaded = false;
                UploadEvent ev = new UploadEvent(() => _uploader.DeleteTournament(tournament.Tournament.SanctionNumber),
                                                 tournament.Tournament, UploadEvent.Type.DeleteTournament, 0);
                UploadQueue.Enqueue(ev);
            }
        }
        public void should_retry_if_upload_fails()
        {
            int attempts = 0;
            var sut      = new UploadQueue <string>(x =>
            {
                attempts++;
                throw new NotSupportedException("we need more power");
            })
            {
                ActivateSync  = true,
                RetryInterval = TimeSpan.FromMilliseconds(10)
            };

            sut.Enqueue("hello");

            sut.Wait(1000);
            attempts.Should().BeGreaterThan(1);
        }
        public void should_not_invoke_directly_if_queue_got_items()
        {
            var uploadIsInvoked = false;
            var directly        = false;
            var sut             = new UploadQueue <string>(x =>
            {
                uploadIsInvoked = true;
                Task.Delay(50).Wait();
            });

            sut.Enqueue("precondition");
            sut.ActivateSync = true;

            sut.EnqueueIfNotEmpty("hello", () => directly = true);

            sut.Wait(10000);
            directly.Should().BeFalse("because there is an active worker");
            uploadIsInvoked.Should()
            .BeTrue(
                "because it should be invoked even if a task is supplied as an argument when the queue is not empty");
        }
        public void should_throw_report_after_max_attempts()
        {
            int attempts = 0;
            var sut      = new UploadQueue <string>(x =>
            {
                attempts++;
                throw new NotSupportedException("we need more power");
            })
            {
                ActivateSync  = true,
                RetryInterval = TimeSpan.FromMilliseconds(10),
                MaxAttempts   = 3
            };
            bool failed = false;

            sut.UploadFailed += (sender, args) => failed = true;

            sut.Enqueue("hello");

            sut.Wait(1000);
            failed.Should().BeTrue();
        }
        public void CheckTournaments()
        {
            AddNewAndActiveTournaments();
            foreach (TrackableTournament t in Tournaments.Where(t => t.Tracking))
            {
                Tournament oldTournament = t.Tournament;
                Tournament newTournament = _reader.getTournament(t.Tournament.TournamentId).WithName(t.Name);
                t.Tournament = newTournament;
                Boolean uploadAll = false;
                if (!t.TournamentUploaded)
                {
                    UploadEvent e = new UploadEvent(() => _uploader.UploadTournament(t), newTournament, UploadEvent.Type.Tournament, 0);
                    UploadQueue.Enqueue(e);
                    t.TournamentUploaded = true;
                    uploadAll            = true;
                }
                if (!oldTournament.Equals(newTournament) || uploadAll)
                {
                    if (!oldTournament.Teams.SequenceEqual(newTournament.Teams) || !newTournament.Teams.IsEmpty && uploadAll)
                    {
                        UploadEvent e = new UploadEvent(() => _uploader.UploadTeams(newTournament.SanctionNumber, newTournament.Teams),
                                                        newTournament, UploadEvent.Type.Teams, 0);
                        UploadQueue.Enqueue(e);
                        uploadAll = true;
                    }
                    if (!oldTournament.Seatings.SequenceEqual(newTournament.Seatings) || !newTournament.Seatings.IsEmpty && uploadAll)
                    {
                        UploadEvent e = new UploadEvent(() => _uploader.UploadSeatings(newTournament.SanctionNumber, newTournament.Seatings),
                                                        newTournament, UploadEvent.Type.Seatings, 0);
                        UploadQueue.Enqueue(e);
                    }
                    if (!oldTournament.Rounds.SequenceEqual(newTournament.Rounds) || !newTournament.Rounds.IsEmpty && uploadAll)
                    {
                        foreach (var round in oldTournament.Rounds.ZipAll(newTournament.Rounds, (r1, r2) => new { OldRound = r1, NewRound = r2 }))
                        {
                            if (round.NewRound == null)
                            {
                                UploadEvent e = new UploadEvent(() => _uploader.DeleteRound(newTournament.SanctionNumber, round.OldRound.Number),
                                                                newTournament, UploadEvent.Type.Round, round.OldRound.Number);
                                UploadQueue.Enqueue(e);
                            }
                            else
                            {
                                if (round.OldRound == null || round.NewRound == null || !round.OldRound.Pairings.SequenceEqual(round.NewRound.Pairings, new Pairing.PairingEqualityComparer()) || round.NewRound != null && uploadAll)
                                {
                                    UploadEvent e = new UploadEvent(() => _uploader.UploadPairings(newTournament.SanctionNumber, round.NewRound.Number, round.NewRound.Playoff, round.NewRound.Pairings),
                                                                    newTournament, UploadEvent.Type.Pairings, round.NewRound.Number);
                                    UploadQueue.Enqueue(e);
                                    uploadAll = true;
                                }
                                if (round.OldRound == null || !round.OldRound.Pairings.Select(p => p.Result).SequenceEqual(round.NewRound.Pairings.Select(p => p.Result)) || round.NewRound != null && uploadAll)
                                {
                                    UploadEvent e = new UploadEvent(() => _uploader.UploadResults(newTournament.SanctionNumber, round.NewRound.Number, round.NewRound.Pairings),
                                                                    newTournament, UploadEvent.Type.Results, round.NewRound.Number);
                                    UploadQueue.Enqueue(e);
                                    uploadAll = true;
                                }
                            }
                        }
                    }
                    if (!oldTournament.Pods.SequenceEqual(newTournament.Pods) || !newTournament.Pods.IsEmpty && uploadAll)
                    {
                        UploadEvent e = new UploadEvent(() => _uploader.UploadPods(newTournament.SanctionNumber, newTournament.Pods),
                                                        newTournament, UploadEvent.Type.Pods, 0);
                        UploadQueue.Enqueue(e);
                    }
                }
            }

            Thread.Sleep(5000);
            new CheckTournamentsDelegate(CheckTournaments).BeginInvoke(null, null);
        }