Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1182,4 +1182,4 @@
}
},
"NestedTypes": {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.Net;
using Microsoft.Azure.Cosmos.Resource.FullFidelity;
using Microsoft.Azure.Documents;
using Newtonsoft.Json;
Expand All @@ -28,12 +29,34 @@ class ChangeFeedMetadata
/// </summary>
[System.Text.Json.Serialization.JsonIgnore]
[Newtonsoft.Json.JsonIgnore]
public DateTime? ConflictResolutionTimestamp => this.ConflictResolutionTimestampInSeconds.HasValue ? UnixEpoch.AddSeconds(this.ConflictResolutionTimestampInSeconds.Value) : null;
public DateTime ConflictResolutionTimestamp => UnixEpoch.AddSeconds(this.ConflictResolutionTimestampInSeconds);

private double conflictResolutionTimestampInSeconds;

[System.Text.Json.Serialization.JsonInclude]
[System.Text.Json.Serialization.JsonPropertyName(ChangeFeedMetadataFields.ConflictResolutionTimestamp)]
[JsonProperty(PropertyName = ChangeFeedMetadataFields.ConflictResolutionTimestamp)]
internal double? ConflictResolutionTimestampInSeconds { get; set; }
[JsonProperty(PropertyName = ChangeFeedMetadataFields.ConflictResolutionTimestamp, Required = Required.Always)]
internal double ConflictResolutionTimestampInSeconds
{
get
{
if (this.conflictResolutionTimestampInSeconds <= 0)
{
throw new System.Text.Json.JsonException(
$"{ChangeFeedMetadataFields.ConflictResolutionTimestamp} not set.");
}
return this.conflictResolutionTimestampInSeconds;
}
set
{
if (value == 0)
{
throw new System.Text.Json.JsonException(
$"{ChangeFeedMetadataFields.ConflictResolutionTimestamp} cannot be zero.");
}
this.conflictResolutionTimestampInSeconds = value;
}
}

/// <summary>
/// The current change's logical sequence number.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,5 +641,106 @@ public async Task WhenACFPInAVADModeUsesWithStartFromBeginningExpectExceptionTes
expected: "Using the 'WithStartFromBeginning' option with ChangeFeedProcessor is not supported with Microsoft.Azure.Cosmos.ChangeFeed.ChangeFeedModeFullFidelity mode.",
actual: exception.Message);
}

[TestMethod]
[Owner("trivediyash")]
[Description("Validates that ConflictResolutionTimestampInSeconds getter throws JsonException when value is zero.")]
public void ValidateConflictResolutionTimestampInSecondsGetterThrowsOnZeroTest()
{
ChangeFeedMetadata metadata = new()
{
Lsn = 374,
OperationType = ChangeFeedOperationType.Create
};

// Accessing the getter should throw JsonException when the value is zero (default)
System.Text.Json.JsonException exception = Assert.ThrowsException<System.Text.Json.JsonException>(() =>
{
double value = metadata.ConflictResolutionTimestampInSeconds;
});

Assert.IsTrue(exception.Message.Contains("crts"));
Assert.IsTrue(exception.Message.Contains("not set"));
}

[TestMethod]
[Owner("trivediyash")]
[Description("Validates that ConflictResolutionTimestampInSeconds setter throws JsonException when attempting to set zero.")]
public void ValidateConflictResolutionTimestampInSecondsSetterThrowsOnZeroTest()
{
ChangeFeedMetadata metadata = new()
{
Lsn = 374,
OperationType = ChangeFeedOperationType.Create
};

// Setting the value to zero should throw JsonException
System.Text.Json.JsonException exception = Assert.ThrowsException<System.Text.Json.JsonException>(() =>
{
metadata.ConflictResolutionTimestampInSeconds = 0;
});

Assert.IsTrue(exception.Message.Contains("crts"));
Assert.IsTrue(exception.Message.Contains("cannot be zero"));
}

[TestMethod]
[Owner("trivediyash")]
[Description("Validates that deserializing ChangeFeedMetadata without crts field or with negative crts throws JsonException when accessing ConflictResolutionTimestampInSeconds.")]
public void ValidateDeserializationWithoutCrtsFieldThrowsOnAccessTest()
{
// JSON without the "crts" field - this should deserialize but accessing ConflictResolutionTimestampInSeconds should throw
string jsonWithoutCrts = @"{
""lsn"": 374,
""operationType"": ""Create"",
""previousImageLSN"": 0,
""timeToLiveExpired"": false
}";

// Deserialize the JSON - this should succeed
ChangeFeedMetadata metadata = System.Text.Json.JsonSerializer.Deserialize<ChangeFeedMetadata>(jsonWithoutCrts);

Assert.IsNotNull(metadata);
Assert.AreEqual(expected: 374, actual: metadata.Lsn);
Assert.AreEqual(expected: ChangeFeedOperationType.Create, actual: metadata.OperationType);

// Accessing ConflictResolutionTimestampInSeconds should throw because the value is zero (not set)
System.Text.Json.JsonException exception = Assert.ThrowsException<System.Text.Json.JsonException>(() =>
{
double value = metadata.ConflictResolutionTimestampInSeconds;
});

Assert.IsTrue(exception.Message.Contains("crts"));
Assert.IsTrue(exception.Message.Contains("not set"));

// Accessing ConflictResolutionTimestamp should also throw since it uses ConflictResolutionTimestampInSeconds
System.Text.Json.JsonException timestampException = Assert.ThrowsException<System.Text.Json.JsonException>(() =>
{
DateTime timestamp = metadata.ConflictResolutionTimestamp;
});

Assert.IsTrue(timestampException.Message.Contains("crts"));
Assert.IsTrue(timestampException.Message.Contains("not set"));

// Test with negative value - deserialize JSON with negative crts
// The setter only validates == 0, so negative values can be deserialized
// But the getter will throw when accessed
string jsonWithNegativeCrts = @"{
""lsn"": 374,
""crts"": -100,
""operationType"": ""Create""
}";

ChangeFeedMetadata metadataWithNegative = System.Text.Json.JsonSerializer.Deserialize<ChangeFeedMetadata>(jsonWithNegativeCrts);

// Accessing the getter should throw JsonException when the value is negative
System.Text.Json.JsonException negativeException = Assert.ThrowsException<System.Text.Json.JsonException>(() =>
{
double value = metadataWithNegative.ConflictResolutionTimestampInSeconds;
});

Assert.IsTrue(negativeException.Message.Contains("crts"));
Assert.IsTrue(negativeException.Message.Contains("not set"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,105 @@ public void ValidateChangeFeedMetadataSerializationCreateWriteTest(bool property
actual: json);
}

[TestMethod]
[Owner("trivediyash")]
[Description("Validates that ConflictResolutionTimestampInSeconds getter throws JsonException when value is zero or negative.")]
public void ValidateConflictResolutionTimestampInSecondsGetterThrowsOnZeroTest()
{
ChangeFeedMetadata metadata = new()
{
Lsn = 374,
OperationType = ChangeFeedOperationType.Create
};

// Accessing the getter should throw JsonException when the value is zero (default)
System.Text.Json.JsonException exception = Assert.ThrowsException<System.Text.Json.JsonException>(() =>
{
double value = metadata.ConflictResolutionTimestampInSeconds;
});

Assert.IsTrue(exception.Message.Contains("crts"));
Assert.IsTrue(exception.Message.Contains("not set"));

// Test with negative value - deserialize JSON with negative crts
string jsonWithNegativeCrts = @"{
""lsn"": 374,
""crts"": -100,
""operationType"": ""Create""
}";

ChangeFeedMetadata metadataWithNegative = System.Text.Json.JsonSerializer.Deserialize<ChangeFeedMetadata>(jsonWithNegativeCrts);

// Accessing the getter should throw JsonException when the value is negative
System.Text.Json.JsonException negativeException = Assert.ThrowsException<System.Text.Json.JsonException>(() =>
{
double value = metadataWithNegative.ConflictResolutionTimestampInSeconds;
});

Assert.IsTrue(negativeException.Message.Contains("crts"));
Assert.IsTrue(negativeException.Message.Contains("not set"));
}

[TestMethod]
[Owner("trivediyash")]
[Description("Validates that ConflictResolutionTimestampInSeconds setter throws JsonException when attempting to set zero.")]
public void ValidateConflictResolutionTimestampInSecondsSetterThrowsOnZeroTest()
{
ChangeFeedMetadata metadata = new()
{
Lsn = 374,
OperationType = ChangeFeedOperationType.Create
};

// Setting the value to zero should throw JsonException
System.Text.Json.JsonException exception = Assert.ThrowsException<System.Text.Json.JsonException>(() =>
{
metadata.ConflictResolutionTimestampInSeconds = 0;
});

Assert.IsTrue(exception.Message.Contains("crts"));
Assert.IsTrue(exception.Message.Contains("cannot be zero"));
}

[TestMethod]
[Owner("trivediyash")]
[Description("Validates that deserializing ChangeFeedMetadata without crts field throws JsonException when accessing ConflictResolutionTimestampInSeconds.")]
public void ValidateDeserializationWithoutCrtsFieldThrowsOnAccessTest()
{
// JSON without the "crts" field - this should deserialize but accessing ConflictResolutionTimestampInSeconds should throw
string jsonWithoutCrts = @"{
""lsn"": 374,
""operationType"": ""Create"",
""previousImageLSN"": 0,
""timeToLiveExpired"": false
}";

// Deserialize the JSON - this should succeed
ChangeFeedMetadata metadata = System.Text.Json.JsonSerializer.Deserialize<ChangeFeedMetadata>(jsonWithoutCrts);

Assert.IsNotNull(metadata);
Assert.AreEqual(expected: 374, actual: metadata.Lsn);
Assert.AreEqual(expected: ChangeFeedOperationType.Create, actual: metadata.OperationType);

// Accessing ConflictResolutionTimestampInSeconds should throw because the value is zero (not set)
System.Text.Json.JsonException exception = Assert.ThrowsException<System.Text.Json.JsonException>(() =>
{
double value = metadata.ConflictResolutionTimestampInSeconds;
});

Assert.IsTrue(exception.Message.Contains("crts"));
Assert.IsTrue(exception.Message.Contains("not set"));

// Accessing ConflictResolutionTimestamp should also throw since it uses ConflictResolutionTimestampInSeconds
System.Text.Json.JsonException timestampException = Assert.ThrowsException<System.Text.Json.JsonException>(() =>
{
DateTime timestamp = metadata.ConflictResolutionTimestamp;
});

Assert.IsTrue(timestampException.Message.Contains("crts"));
Assert.IsTrue(timestampException.Message.Contains("not set"));
}

[TestMethod]
[Timeout(300000)]
[TestCategory("LongRunning")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,18 @@
],
"MethodInfo": "System.Collections.Generic.Dictionary`2[System.String,System.Object] PartitionKey;CanRead:True;CanWrite:True;System.Collections.Generic.Dictionary`2[System.String,System.Object] get_PartitionKey();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Nullable`1[System.DateTime] ConflictResolutionTimestamp[System.Text.Json.Serialization.JsonIgnoreAttribute()]-[Newtonsoft.Json.JsonIgnoreAttribute()]": {
"System.DateTime ConflictResolutionTimestamp[System.Text.Json.Serialization.JsonIgnoreAttribute()]-[Newtonsoft.Json.JsonIgnoreAttribute()]": {
"Type": "Property",
"Attributes": [
"JsonIgnoreAttribute",
"JsonIgnoreAttribute"
],
"MethodInfo": "System.Nullable`1[System.DateTime] ConflictResolutionTimestamp;CanRead:True;CanWrite:False;System.Nullable`1[System.DateTime] get_ConflictResolutionTimestamp();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
"MethodInfo": "System.DateTime ConflictResolutionTimestamp;CanRead:True;CanWrite:False;System.DateTime get_ConflictResolutionTimestamp();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Nullable`1[System.DateTime] get_ConflictResolutionTimestamp()": {
"System.DateTime get_ConflictResolutionTimestamp()": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Nullable`1[System.DateTime] get_ConflictResolutionTimestamp();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
"MethodInfo": "System.DateTime get_ConflictResolutionTimestamp();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.String get_Id()[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
Expand Down