xunit icon indicating copy to clipboard operation
xunit copied to clipboard

Structs passed via [MemberData(...)] may be corrupted on Ubuntu

Open Andy-Wilkinson opened this issue 9 years ago • 6 comments

When running xUnit tests on the .Net CLI using the public Travis CI servers (running Ubuntu), data held within a simple struct containing four float fields is corrupted when passed to theory using the [MemberData(...)] attribute. When running the same tests on a local Windows machine, or via AppVeyor (Windows CI) all tests pass as expected.

Example struct,

public struct Vector
{
    public float X;
    public float Y;
    public float Z;
    public float W;

    public Vector(float x, float y, float z, float w)
    {
        W = w;
        X = x;
        Y = y;
        Z = z;
    }
}

Usage,

public static object[][] ArrayVectorData = new[] { new object[] { new Vector(1, 2, 3, 4) } };

[Theory]
[MemberData(nameof(ArrayVectorData))]
public void Vector_ViaArrayMemberData(Vector vector)
{
    Assert.Equal(1, vector.X);
    Assert.Equal(2, vector.Y);
    Assert.Equal(3, vector.Z);
    Assert.Equal(4, vector.W);
}

On Windows: Test passes. On Travis CI: Expected: 3, Actual: -2.511178E-26

I've created a simple repro at, Andy-Wilkinson/BugRepro-VectorTests The corresponding CI logs show Travis CI failing (log) and AppVeyor passing (log)

Andy-Wilkinson avatar Jan 08 '17 19:01 Andy-Wilkinson

This is probably an issue with the Core CLR that belongs on that repo. The xUnit libraries are .NET Standard and there's nothing special we do x-plat. If there's an issue on a particular platform, it's probably a runtime issue.

clairernovotny avatar Jan 08 '17 20:01 clairernovotny

Thanks for the comment @onovotny. It does sound like something the Core CLR is doing differently between Windows and Ubuntu. I tried accessing the array and unboxing the struct myself, and this seemed to work fine. Is there anything clever that xUnit does between extracting a value from the MemberData attribute and passing it into the test method?

Andy-Wilkinson avatar Jan 08 '17 20:01 Andy-Wilkinson

All items with xUnit use a custom serializer since binary serialization doesn't exist on CoreCLR:

https://github.com/xunit/xunit/blob/c66f07f66be807e7a75b811e11e7b3b49cfac11c/src/common/XunitSerializationInfo.cs

It's likely that Vector isn't directly supported, but then I wouldn't expect it to work on CoreCLR/Windows either. 

clairernovotny avatar Jan 08 '17 20:01 clairernovotny

This seems like it might be of interest to the CoreCLR team if it really is a bug in CoreCLR. I've created an issue that links to this at dotnet/coreclr#9031

jamesqo avatar Jan 20 '17 23:01 jamesqo

@jamesqo Thanks for posting this over at the coreclr repo. There shouldn't be cross-platform issues if the CLR is consistent (I think!)

Andy-Wilkinson avatar Jan 29 '17 11:01 Andy-Wilkinson

I've managed to get Travis CI to update to the latest version of the dotnet SDK (1.0.0-rc4-004771) and the tests run without error now (build logs here ). Looks like whatever the underlying cause was has been fixed. :smile:

Andy-Wilkinson avatar Feb 16 '17 21:02 Andy-Wilkinson