Пример #1
1
 public void ToDateTime_OutOfRange(long seconds, int nanoseconds)
 {
     var value = new Timestamp { Seconds = seconds, Nanos = nanoseconds };
     Assert.Throws<InvalidOperationException>(() => value.ToDateTime());
 }
Пример #2
0
        public void ToDateTimeTruncation()
        {
            var t1 = new Timestamp { Seconds = 1, Nanos = 1000000 + Duration.NanosecondsPerTick - 1 };
            Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 1, DateTimeKind.Utc).AddMilliseconds(1), t1.ToDateTime());

            var t2 = new Timestamp { Seconds = -1, Nanos = 1000000 + Duration.NanosecondsPerTick - 1 };
            Assert.AreEqual(new DateTime(1969, 12, 31, 23, 59, 59).AddMilliseconds(1), t2.ToDateTime());
        }
Пример #3
0
        public static DateTime ToDateTime(this Timestamp timestamp)
        {
            var protoTimestamp = new Google.Protobuf.WellKnownTypes.Timestamp
            {
                Nanos   = timestamp.Nanos,
                Seconds = timestamp.Seconds
            };

            return(protoTimestamp.ToDateTime());
        }
Пример #4
0
 public void Arithmetic()
 {
     Timestamp t1 = new Timestamp { Seconds = 10000, Nanos = 5000 };
     Timestamp t2 = new Timestamp { Seconds = 8000, Nanos = 10000 };
     Duration difference = new Duration { Seconds = 1999, Nanos = Duration.NanosecondsPerSecond - 5000 };
     Assert.AreEqual(difference, t1 - t2);
     Assert.AreEqual(-difference, t2 - t1);
     
     Assert.AreEqual(t1, t2 + difference);
     Assert.AreEqual(t2, t1 - difference);
 }
Пример #5
0
 public void TimestampStandalone_NonNormalized(long seconds, int nanoseconds)
 {
     var timestamp = new Timestamp { Seconds = seconds, Nanos = nanoseconds };
     Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(timestamp));
 }
Пример #6
0
 public void ToDateTime_ValidBoundaries(long seconds, int nanoseconds)
 {
     var value = new Timestamp { Seconds = seconds, Nanos = nanoseconds };
     value.ToDateTime();
 }
Пример #7
0
 public static Timestamp ToTimestamp(this DateTime dateTime)
 {
     return(Timestamp.FromDateTime(dateTime));
 }
Пример #8
0
 public static Timestamp ToTimestamp(this DateTimeOffset dateTimeOffset)
 {
     return(Timestamp.FromDateTimeOffset(dateTimeOffset));
 }
Пример #9
0
        private static void StartProducing(ILogger logger, IConfiguration config, SchemaRegistryConfig schemaRegistryConfig)
        {
            var cachedSchemaRegistryClient = new CachedSchemaRegistryClient(schemaRegistryConfig);
            var producerFactory            = new ProducerFactory <string, Record>(
                logger,
                new ProtobufSerializer <Record>(cachedSchemaRegistryClient),
                KafkaOptions.ForProducer(config));

            var kafkaProducer = producerFactory.CreateProducer();

            for (int i = 0; i < 10; i++)
            {
                var person = new Person
                {
                    Id          = i,
                    Name        = $"{nameof(Person.Name)} {i}",
                    Email       = $"{nameof(Person.Email)} {i}",
                    Age         = 20 + i,
                    PhoneNumber = $"{nameof(Person.PhoneNumber)} {i}"
                };

                var address = new Address
                {
                    PersonId = i,
                    State    = $"{nameof(Address.State)} {i}",
                    Street   = $"{nameof(Address.Street)} {i}",
                    ZipCode  = $"{nameof(Address.ZipCode)} {i}",
                };

                var personRecord = new Record {
                    CreatedDate = Timestamp.FromDateTime(DateTime.UtcNow), Id = Guid.NewGuid().ToString(), Payload = Any.Pack(person)
                };
                var addressRecord = new Record {
                    CreatedDate = Timestamp.FromDateTime(DateTime.UtcNow), Id = Guid.NewGuid().ToString(), Payload = Any.Pack(address)
                };

                var personMessage = new Confluent.Kafka.Message <string, Record>
                {
                    Key   = Guid.NewGuid().ToString(),
                    Value = personRecord
                };

                var addressMessage = new Message <string, Record>
                {
                    Key   = Guid.NewGuid().ToString(),
                    Value = addressRecord
                };

                logger.Information(
                    "Sending message => Topic: {Topic} Key: {Key} Value: {Value}",
                    PERSON_TOPIC,
                    personMessage.Key,
                    personMessage.Value);

                kafkaProducer.ProduceAsync(PERSON_TOPIC, personMessage)
                .GetAwaiter()
                .GetResult();

                logger.Information(
                    "Sending message => Topic: {Topic} Key: {Key} Value: {Value}",
                    PERSON_TOPIC,
                    addressMessage.Key,
                    addressMessage.Value);

                kafkaProducer.ProduceAsync(PERSON_TOPIC, addressMessage)
                .GetAwaiter()
                .GetResult();
            }
        }
Пример #10
0
        private void WriteTimestamp(StringBuilder builder, IMessage value)
        {
            builder.Append('"');
            // TODO: In the common case where this *is* using the built-in Timestamp type, we could
            // avoid all the reflection at this point, by casting to Timestamp. In the interests of
            // avoiding subtle bugs, don't do that until we've implemented DynamicMessage so that we can prove
            // it still works in that case.
            int nanos = (int) value.Descriptor.Fields[Timestamp.NanosFieldNumber].Accessor.GetValue(value);
            long seconds = (long) value.Descriptor.Fields[Timestamp.SecondsFieldNumber].Accessor.GetValue(value);

            // Even if the original message isn't using the built-in classes, we can still build one... and its
            // conversion will check whether or not it's normalized.
            // TODO: Perhaps the diagnostic-only formatter should not throw for non-normalized values?
            Timestamp ts = new Timestamp { Seconds = seconds, Nanos = nanos };
            // Use .NET's formatting for the value down to the second, including an opening double quote (as it's a string value)
            DateTime dateTime = ts.ToDateTime();
            builder.Append(dateTime.ToString("yyyy'-'MM'-'dd'T'HH:mm:ss", CultureInfo.InvariantCulture));
            AppendNanoseconds(builder, Math.Abs(ts.Nanos));
            builder.Append("Z\"");
        }
Пример #11
0
 public void ToString_NonNormalized()
 {
     // Just a single example should be sufficient...
     var duration = new Timestamp { Seconds = 1, Nanos = -1 };
     Assert.AreEqual("{ \"@warning\": \"Invalid Timestamp\", \"seconds\": \"1\", \"nanos\": -1 }", duration.ToString());
 }
Пример #12
-1
 private static void AssertRoundtrip(Timestamp timestamp, DateTime dateTime)
 {
     Assert.AreEqual(timestamp, Timestamp.FromDateTime(dateTime));
     Assert.AreEqual(dateTime, timestamp.ToDateTime());
     Assert.AreEqual(DateTimeKind.Utc, timestamp.ToDateTime().Kind);
 }