Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Add more tests for collections#41977

Closed
layomia wants to merge 1 commit into
dotnet:masterfrom
layomia:tests
Closed

Add more tests for collections#41977
layomia wants to merge 1 commit into
dotnet:masterfrom
layomia:tests

Conversation

@layomia

@layomia layomia commented Oct 22, 2019

Copy link
Copy Markdown
Contributor

Additional tests for collections, given the changes in #41482.

@layomia layomia added this to the 5.0 milestone Oct 22, 2019
@layomia layomia self-assigned this Oct 22, 2019
Assert.Equal(1, qs.Count);
qc.TryPeek(out val);
Assert.Equal("1", val);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ConcurrentBag<T> as well?

Assert.Throws<NotSupportedException>(() => JsonSerializer.Deserialize<BlockingCollection<string>>(@"[""1""]"));

// Not supported. Not IList, and we don't detect the add method for this collection.
Assert.Throws<NotSupportedException>(() => JsonSerializer.Deserialize<ConcurrentBag<string>>(@"[""1""]"));

@stephentoub stephentoub Oct 22, 2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why isn't ConcurrentBag<T> supported but ConcurrentQueue<T> and ConcurrentStack<T> are? All three implement the same two interfaces: IProducerConsumerCollection<T> and IReadOnlyCollection<T>.

public class ConcurrentStack<T> : IProducerConsumerCollection<T>, IReadOnlyCollection<T>

public class ConcurrentBag<T> : IProducerConsumerCollection<T>, IReadOnlyCollection<T>

public class ConcurrentQueue<T> : IProducerConsumerCollection<T>, IReadOnlyCollection<T>

Also the comment says "we don't detect the add method for this collection"... why do we detect Enqueue on ConcurrentQueue<T> and Push on ConcurrentStack<T> but not Add on ConcurrentBag<T>?

Without knowing more, this seems like a bug rather than something to encode in a test.

public static void Read_SpecializedCollection_Throws()
{
// Add method for this collection only accepts strings, even though it only implements IList which usually
// indicates that the element type is typeof(object).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems like a strange limitation; we support non-generic collections... why is it a problem for the Add method to support a more derived type?

{
Assert.Equal(@"[""1""]", JsonSerializer.Serialize(new BlockingCollection<string> { "1" }));

Assert.Equal(@"[""1""]", JsonSerializer.Serialize(new ConcurrentBag<string> { "1" }));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So we support serializing some types but not deserializing them?

[Fact]
public static void Read_ConcurrentCollection()
{
ConcurrentDictionary<string, string> cd = JsonSerializer.Deserialize<ConcurrentDictionary<string, string>>(@"{""key"":""value""}");

@steveharter steveharter Oct 22, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IMO having a separate test for each type doing both deserialize + serialize as well as verifying round-tripping capability (e.g. an extra serialize or deserialize) would be more readable and better test the functionality.

I realize some existing tests separate the "read" and "write", but going forward I don't think that is better.

@steveharter

Copy link
Copy Markdown
Contributor

As part of the "auditing" PR it would be goodness to have that PR display what collection types are supported and what is not -- e.g. a list of all collection types and interfaces, can they be derived from and whether they support both serialize and deserialize.

@maryamariyan

maryamariyan commented Nov 6, 2019

Copy link
Copy Markdown

Thank you for your contribution. As announced in dotnet/coreclr#27549 this repository will be moving to dotnet/runtime on November 13. If you would like to continue working on this PR after this date, the easiest way to move the change to dotnet/runtime is:

  1. In your corefx repository clone, create patch by running git format-patch origin
  2. In your runtime repository clone, apply the patch by running git apply --directory src/libraries <path to the patch created in step 1>

@ericstj

ericstj commented Nov 12, 2019

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 4 pipeline(s).

@ahsonkhan ahsonkhan added the test enhancement Improvements of test source code label Nov 13, 2019
@ahsonkhan

Copy link
Copy Markdown

Looks like this PR has some questions/discussion worth following up on. We can consider closing this and re-opening it in dotnet/runtime, when ready.

@layomia

layomia commented Nov 13, 2019

Copy link
Copy Markdown
Contributor Author

Looks like this PR has some questions/discussion worth following up on. We can consider closing this and re-opening it in dotnet/runtime, when ready.

Closing for this reason. Will address feedback shortly in dotnet/runtime.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Text.Json test enhancement Improvements of test source code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Audit JSON (de)serialization behavior for System.Collections.Concurrent

6 participants