Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion Microsoft.Azure.Cosmos/src/Headers/Headers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ internal Headers Clone()
Headers clone = new Headers();
foreach (string key in this.CosmosMessageHeaders.AllKeys())
{
clone.Add(key, this.CosmosMessageHeaders.Get(key));
string value = this.CosmosMessageHeaders.Get(key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it expected that there be nulls in there? Instead of (or as well as) this should we be investigating how the null got in there?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed that it's a concurrency issue, otherwise +1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What concurrency issue? Someone is writing to the headers collection on one thread while we're reading? That's not safe.

if (value != null) clone.Add(key, this.CosmosMessageHeaders.Get(key));
}

return clone;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,86 @@ public void TestAllKeysWithKnownProperties()
Assert.IsTrue(allKeys.Contains(HttpConstants.HttpHeaders.Continuation));
Assert.IsTrue(allKeys.Contains(WFConstants.BackendHeaders.SubStatus));
}

[TestMethod]
public void TestHeadersClone()
{
Headers Headers = new Headers();
Headers.CosmosMessageHeaders[Key] = Guid.NewGuid().ToString();
Headers.ContinuationToken = Guid.NewGuid().ToString();
Headers.CosmosMessageHeaders[HttpConstants.HttpHeaders.RetryAfterInMilliseconds] = "20";
Headers.Add(WFConstants.BackendHeaders.SubStatus, "1002");
Headers.PartitionKey = Guid.NewGuid().ToString();
string[] allKeys = Headers.AllKeys();
Assert.IsTrue(allKeys.Contains(Key));
Assert.IsTrue(allKeys.Contains(HttpConstants.HttpHeaders.PartitionKey));
Assert.IsTrue(allKeys.Contains(HttpConstants.HttpHeaders.RetryAfterInMilliseconds));
Assert.IsTrue(allKeys.Contains(HttpConstants.HttpHeaders.Continuation));
Assert.IsTrue(allKeys.Contains(WFConstants.BackendHeaders.SubStatus));

Headers clone = Headers.Clone();
Assert.IsNotNull(clone);
string[] keys = clone.AllKeys();
Assert.IsTrue(keys.Contains(Key));
Assert.IsTrue(keys.Contains(HttpConstants.HttpHeaders.PartitionKey));
Assert.IsTrue(keys.Contains(HttpConstants.HttpHeaders.RetryAfterInMilliseconds));
Assert.IsTrue(keys.Contains(HttpConstants.HttpHeaders.Continuation));
Assert.IsTrue(keys.Contains(WFConstants.BackendHeaders.SubStatus));
Assert.AreEqual(allKeys.Length, keys.Length);
Assert.AreEqual(
Headers.Get(HttpConstants.HttpHeaders.PartitionKey),
clone.Get(HttpConstants.HttpHeaders.PartitionKey));
Assert.AreEqual(
Headers.Get(HttpConstants.HttpHeaders.RetryAfterInMilliseconds),
clone.Get(HttpConstants.HttpHeaders.RetryAfterInMilliseconds));
Assert.AreEqual(
Headers.Get(HttpConstants.HttpHeaders.Continuation),
clone.Get(HttpConstants.HttpHeaders.Continuation));
Assert.AreEqual(
Headers.Get(WFConstants.BackendHeaders.SubStatus),
clone.Get(WFConstants.BackendHeaders.SubStatus));
}

[TestMethod]
public void TestNullHeadersClone()
{
Headers Headers = new Headers();
Headers.CosmosMessageHeaders[Key] = Guid.NewGuid().ToString();
Headers.ContinuationToken = Guid.NewGuid().ToString();
Headers.CosmosMessageHeaders[HttpConstants.HttpHeaders.RetryAfterInMilliseconds] = "20";
Headers.Add(WFConstants.BackendHeaders.SubStatus, "1002");
Headers.PartitionKey = "pk";

Headers.CosmosMessageHeaders["x-ms-documentdb-partitionkey"] = null;

string[] allKeys = Headers.AllKeys();
Assert.IsTrue(allKeys.Contains(Key));
Assert.IsFalse(allKeys.Contains(HttpConstants.HttpHeaders.PartitionKey));
Assert.IsTrue(allKeys.Contains(HttpConstants.HttpHeaders.RetryAfterInMilliseconds));
Assert.IsTrue(allKeys.Contains(HttpConstants.HttpHeaders.Continuation));
Assert.IsTrue(allKeys.Contains(WFConstants.BackendHeaders.SubStatus));

Headers clone = Headers.Clone();
Assert.IsNotNull(clone);
string[] keys = clone.AllKeys();
Assert.IsTrue(keys.Contains(Key));
Assert.IsFalse(keys.Contains(HttpConstants.HttpHeaders.PartitionKey));
Assert.IsTrue(keys.Contains(HttpConstants.HttpHeaders.RetryAfterInMilliseconds));
Assert.IsTrue(keys.Contains(HttpConstants.HttpHeaders.Continuation));
Assert.IsTrue(keys.Contains(WFConstants.BackendHeaders.SubStatus));
Assert.AreEqual(allKeys.Length, keys.Length);
Assert.AreEqual(
Headers.Get(HttpConstants.HttpHeaders.PartitionKey),
clone.Get(HttpConstants.HttpHeaders.PartitionKey));
Assert.AreEqual(
Headers.Get(HttpConstants.HttpHeaders.RetryAfterInMilliseconds),
clone.Get(HttpConstants.HttpHeaders.RetryAfterInMilliseconds));
Assert.AreEqual(
Headers.Get(HttpConstants.HttpHeaders.Continuation),
clone.Get(HttpConstants.HttpHeaders.Continuation));
Assert.AreEqual(
Headers.Get(WFConstants.BackendHeaders.SubStatus),
clone.Get(WFConstants.BackendHeaders.SubStatus));
}
}
}
Loading