runtime icon indicating copy to clipboard operation
runtime copied to clipboard

How should we handle default data for Globalization tests?

Open ellismg opened this issue 11 years ago • 13 comments

In .NET Core, a large portion of the implementation of our globalization code is built on-top of OS primitives. which we don't control. Globalization data changes frequently and updates to the data can break unit tests.

Some examples of changes we've had problems with in the past:

  1. Collation data changing across releases and platforms, causing sorting tests to break.
  2. Locale data changing (e.g a locale adopts a new currency symbol) causing test breaks.
  3. Casing data changes, breaking ToUpper/ToLower tests.
  4. Different platforms supporting different version of IDNA.

In our internal tests, we've taken a few approaches to solving these problems:

  1. Implementing another set of (hopefully lightweight) P/Invokes on-top of OS Functions and using them to generate the expected data at runtime.
  2. Disabling specific tests (by hand) on specific platforms or changing the expected data for a specific platform via a runtime test.
  3. Encoding metadata about the version of Unicode a platform supports and the version a test requires and skipping tests if they don't match.

As I start to open source more of our collation tests, I'm running into problems where we depend on support libraries that implement the OS behavior from (1). It's not clear to me that adopting this is the right long term approach. There's a bunch of platform specific code in these support libraries which will also have to be brought cross platform. The library also contains a fair amount of logic itself.

My general preference would be to do something like the following:

  1. Use XUnit data driven tests (i.e. [Theory]) to write tests that decouple the implementation of the test from the input data.
  2. Using [MemberData] control the set of inputs that flow to each test based on runtime platform detection. For example, consider something like the following:
    [Theory]
    [MemberData("CompareToData")]
    public static void CompareTo(string localeName, string left, string right, int expected, CompareOptions options)
    {
        CompareInfo ci = CompareInfo.GetCompareInfo(localeName);

        int result = ci.Compare(left, right, options);

        Assert.Equal(expected, Math.Sign(result));
    }

    public static IEnumerable<object[]> CompareToData
    {
        get
        {
            yield return new object[] { "", "Works Everywhere", "WORKS EVERYWHERE", 0, CompareOptions.IgnoreCase };

            if (PlatformDetection.IsWindows)
            {
                yield return /* ... some set of data that only works on Windows ... */;
            }
        }
    }

Will will also need the ability to run certain cases on a specific version of Windows (since behavior changes between versions) or a specific version of ICU. However, I would expect the general shape of the tests would remain the same ([Theory]'s + [MemberData]'s) and the only thing that would differ would be the filtering we use when deciding if we include a test case in the MemberData.

I think there are two downsides with this approach:

  1. Bringing a new version of an existing platform online is more expensive. Since we no longer call to the OS to get our oracle data, we will need to investigate test failures to understand if there are product bugs or simply changes in OS algorithms.
  2. [Theory] is slower than simply having a ton of a Assert's for different cases (or doing a loop) in one [Fact]. The major slowdown was in test discovery. As an experiment (and to ease some debugging for some work I was doing), I ported the NormalizationAll test to use [Theory] and the test which used complete very quickly now take about a minute to run (There are 72K test cases, XUnit reports that it took 10.6 seconds to execute the tests (compared to 1.5 when using a loop), but that didn't count the ~50 seconds to discover the tests). It also appears that much of this time is spent even if the tests would not be run (e.g due to [ActiveIssue] or [OuterLoop]). Ideally this could be fixed, but I am not sure, naively it feels weird that you pay an order of magnitude penalty in test execution because of Theory, but I have not dug into the internals of xUnit. The mitigation here is to not use [Theory] when doing so would generate tons of cases, I guess, and just do a loop ourselves (ideally still over something like a member data API).

I'm not sure if these are dealbreakers or not. (2) seems bad, but is a general problem for us adopting Theory for large data-sets, which seems to be a a problem for more than just globalization. I also really hope that we can adopt something like this, since it makes reporting for data driven tests so much nicer.

I'm interested in what @stephentoub and @tarekgh think here. Does the pain of OS Data updates make the OS Wrapper library the right approach, even if it brings in a lot of extra code and causes a another large amount of work when we bring up a new platform? Is the fixed data + filtering approach a better long term direction? Do we need both for some reason?

ellismg avatar Jun 18 '15 23:06 ellismg

I'm interested in what @stephentoub and @tarekgh think here

I like your proposed direction. With regards to the downsides, seems like you'd have the first issue regardless of how the data is surfaced to the test, since by definition OS-specific behaviors would need to have OS-specific test data. Plus, by hiding this behind a MemberData abstraction, if the OS did provide a mechanism whereby it could be queried for appropriate data, that could easily be plugged in. With regards to the xunit issue...

Ideally this could be fixed

I just did some very quick profiling, and it definitely looks fixable, with a large portion of the time spent in serialization that looks like it could be avoided. I'll dig deeper later, but at least for now I don't think we should let this get in our way.

stephentoub avatar Jun 19 '15 00:06 stephentoub

This was somewhat on my mind, because I did a bit of work on System.Text.Encoding.CodePages. We may want custom discoverers anyways, to do things like pull data from files; we might have an easier time finding/getting/building .csvs for things like upper/lowercase conversion (or even an outright in-process database for this purpose)

@stephentoub - The serialization appears to have been deliberate (allowed running the same cases again). I haven't looked into what happens if this is used in multiple places (if it reuses data sets), like I do for PLINQ (...although a number of mine pass parameters anyways). You can turn off enumeration during discovery, which might stop the serialization?

Clockwork-Muse avatar Jun 19 '15 03:06 Clockwork-Muse

adding @venkat-raman251 as he is working to port the globalization tests. @ellismg it'll be good to sync up with him too.

in general I am seeing we have 2 parts for the globalization tests. part which we need to validate the framework behavior against the OS. and the other part is validating the behavior in general on whatever OS we are running on. The first one, I think we have to get the OS data/behavior any way. I believe this is important as we want to make sure the framework is not doing any bad when reading the OS data. I already have a test for Windows testing that for all culture data. but not testing the sorting/normalization behavior. if we write such tests for the other platforms (Linux/iOS) it should be good enough and I don't expect we'll touch such tests again. The second part which is testing the behavior, this will include the cases which we can feed the data (using the Theory methods) and we should test against expected behavior regardless of the OS. for example, testing the currency number formatting, we supply the currency symbol to the theory method and we should expect well defined output regardless the OS.

for sorting/Normalization testing, I understand it is harder to handle but we should come up with the common cases that should behave the same in all platforms. (like a < b is always true for instance). for other cases that the OS has different behavior we should do it as the first part (call the OS and compare it to the framework result).

tarekgh avatar Jun 19 '15 16:06 tarekgh

The serialization appears to have been deliberate

Testing for serializability, yes, but the actual act of serializing appears to be unnecessary (and costly). I'll look into it more later and submit a PR as is appropriate.

stephentoub avatar Jun 19 '15 16:06 stephentoub

xunit PR here: https://github.com/xunit/xunit/pull/441

stephentoub avatar Jun 20 '15 22:06 stephentoub

Just FYI, the xunit changes got merged.

stephentoub avatar Jun 28 '15 22:06 stephentoub

@ellismg Unless you think it would make the tests pointlessly soft, I was thinking that it should just be more forgiving, e.g.

[Theory]
[MemberData(nameof(CalendarWeekRule_TestData))]
public void CalendarWeekRuleTest(string culture, CalendarWeekRule[] acceptableAnswers)
{
    CultureInfo cultureInfo = new CultureInfo(culture);
    DateTimeFormatInfo dateTimeFormatInfo = cultureInfo.DateTimeFormat;
    Assert.Contains(dateTimeFormatInfo.CalendarWeekRule, acceptableAnswers);
}

public static IEnumerable<object[]> CalendarWeekRule_TestData()
{
    return new object[][]
    {
        new object[]
        {
            string.Empty,
            new[] { CalendarWeekRule.FirstDay },
        },
        new object[]
        {
            "en-US",
            new[] { CalendarWeekRule.FirstDay },
        },
        new object[]
        {
            "br-FR",
            // br-FR changed in [whenver they changed], so depending on the OS/ICU data
            // available one of FirstFullWeek or FirstFourDayWeek should be the answer.
            new[] { CalendarWeekRule.FirstFullWeek, CalendarWeekRule.FirstFourDayWeek },
         },
     };
}

Any time that it's important where we distinguish the "right" answer could still be done; and if it just matters that it be internally consistent, that's also possible to codify as a separate test.

bartonjs avatar Feb 22 '16 16:02 bartonjs

@ellismg has my work cleaning up the tests (and @tarekgh's recent stuff) meant that this issue can be closed?

If not, what remains? Thanks

hughbe avatar May 15 '16 14:05 hughbe

I think we are in a good place now. Thanks for everyone's hard work.

ellismg avatar May 15 '16 19:05 ellismg

https://github.com/dotnet/corefx/blob/master/src/System.Globalization/tests/DateTimeFormatInfo/DateTimeFormatInfoGetAbbreviatedDayName.cs#L19 https://github.com/dotnet/corefx/blob/master/src/System.Globalization/tests/DateTimeFormatInfo/DateTimeFormatInfoGetAbbreviatedMonthName.cs#L22 https://github.com/dotnet/corefx/blob/master/src/System.Globalization/tests/DateTimeFormatInfo/DateTimeFormatInfoGetDayName.cs#L20 https://github.com/dotnet/corefx/blob/master/src/System.Globalization/tests/DateTimeFormatInfo/DateTimeFormatInfoGetMonthName.cs#L22

Disabled test, reopen it.

KristinXie1 avatar Mar 06 '17 02:03 KristinXie1

Nothing more need to be done here.

tarekgh avatar Mar 06 '17 16:03 tarekgh

@tarekgh do we need to remove the comment:

            // TODO: when dotnet/corefx#2103 is addressed, we should also check fr-FR
            if (!PlatformDetection.IsUbuntu1510 && !PlatformDetection.IsUbuntu1604 && !PlatformDetection.IsUbuntu1610
                && !PlatformDetection.IsWindows7 && !PlatformDetection.IsWindows8x && !PlatformDetection.IsFedora24)
            {
                yield return new object[] { new CultureInfo("ur-IN").NumberFormat, NumberFormatInfoData.UrINNumberGroupSizes() };
            }
        }

danmoseley avatar Mar 29 '17 16:03 danmoseley

yes we need to remove the comment. I'll try to do that.

tarekgh avatar Mar 29 '17 16:03 tarekgh