Пример #1
0
        internal void BatchIDTests(BatchID id, DateTime timestamp, bool result)
        {
            WHEN["creating an instance with invalid Timestamp (before MinTimestamp)", ThenIsThrown <InvalidOperationException>()] = () =>
                                                                                                                                    new BatchID(new DateTime(2015, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(-1));

            WHEN["creating an instance with invalid Timestamp (after MaxTimestamp)", ThenIsThrown <InvalidOperationException>()] = () =>
                                                                                                                                   new BatchID(new DateTime(2083, 1, 19, 3, 14, 7, DateTimeKind.Utc).AddMilliseconds(1));

            WHEN["creating an instance with a local timezoned timestamp", ThenIsThrown <InvalidOperationException>()] = () =>
                                                                                                                        new BatchID(DateTime.Now);

            GIVEN["a new BatchID"]    = () => id = new BatchID(timestamp = DateTime.UtcNow);
            THEN["its Sequence is 1"] = () => id.Sequence.Should().Be(1);
            AND["its timestamp is rounded to 10ms"] = () => (id.Timestamp.Ticks % (TimeSpan.TicksPerMillisecond * 10)).Should().Be(0);

            WHEN["calling TryToAdvance with same timestamp"] = () => result = id.TryToAdvance(timestamp);
            THEN["it should return true"] = () => result.Should().BeTrue();
            AND["its Sequence is 2"]      = () => id.Sequence.Should().Be(2);

            GIVEN["a BatchID with a sequence one below maximum sequence"] = () => id = BatchID.DeserializeInternal(EventID.GetTimestampDate(timestamp), 510);
            WHEN["calling TryToAdvance"]     = () => id.TryToAdvance(timestamp);
            THEN["Sequence is max sequence"] = () => id.Sequence.Should().Be(511);

            WHEN["calling TryToAdvance once more"] = () => result = id.TryToAdvance(timestamp);
            THEN["it returns false"] = () => result.Should().BeFalse();
            AND["Sequence is max"]   = () => id.Sequence.Should().Be(511);

            WHEN["calling TryToAdvance with timestamp + 10 ticks"] = () => result = id.TryToAdvance(timestamp.AddTicks(10));
            THEN["it still returns false"] = () => result.Should().BeFalse();

            WHEN["calling TryToAdvance with timestamp + 10ms"] = () => result = id.TryToAdvance(timestamp = timestamp.AddMilliseconds(10));
            THEN["it returns true"] = () => result.Should().BeTrue();
            AND["its Timestamp is set to the new timestamp"] = () => id.Timestamp.Should().Be(EventID.GetTimestampDate(timestamp));
            AND["its Sequence should be 1"] = () => id.Sequence.Should().Be(1);

            WHEN["calling TryToAdvance with an invalid timestamp", ThenIsThrown <InvalidOperationException>()] = () =>
                                                                                                                 id.TryToAdvance(new DateTime(2090, 1, 1));

            WHEN["calling TryToAdvance with a timestamp before the current timestamp", ThenIsThrown <InvalidOperationException>()] = () =>
                                                                                                                                     id.TryToAdvance(timestamp.AddMilliseconds(-10));
        }