async/await always freezes :(

vlad.7041vlad.7041 USMember
edited November 2014 in General

Hi, all!

Here is a tiny reproduction:

    [Test] // THIS WORKS FINE
    public void MicrosoftCom_Via_HttpClient_OldSchool ()
    {
        Console.WriteLine ("Hi from Console");
        Debug.WriteLine ("Hi from Debug");
        using (HttpClient client = new HttpClient ()) 
        {
            byte[] bytes = client.GetByteArrayAsync("http://microsoft.com").Result;
            Assert.IsNotNull (bytes);
            Assert.IsTrue (bytes.Length > 0);
            Debug.WriteLine ("Content length: " + bytes.Length);
        }
    }

    [Test] // THIS TEST NEVER FINISHES. Its frezing
    public void MicrosoftCom_Via_HttpClient_Modern ()
    {
        MicrosoftCom_Via_HttpClient_Impl().Wait ();
    }

    private async Task MicrosoftCom_Via_HttpClient_Impl ()
    {
        Console.WriteLine ("Hi from Console");
        Debug.WriteLine ("Hi from Debug");
        using (HttpClient client = new HttpClient ()) 
        {
            byte[] bytes = await client.GetByteArrayAsync("http://microsoft.com");
            Assert.IsNotNull (bytes);
            Assert.IsTrue (bytes.Length > 0);
            Debug.WriteLine ("Content length: " + bytes.Length);
        }
    }

HttpClient portable library was added via nuget. App is android unit tests.
Both tests run the same async method: HttpClient.GetByteArrayAsync
It seems async/await doesn't supported in some cases :(
Is there working sample, which works using async/await inside?

Answers

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    I think your test blocks itself. What happens if you change your test to:

        [Test]
        public async void MicrosoftCom_Via_HttpClient_Modern ()
        {
            await MicrosoftCom_Via_HttpClient_Impl();
        }
    

    I also think you get the point of testing wrong. The method you are testing should not contain testing-framework methods and such, because your test is to prove and strengthen the method. Also your test-method should be named after what it is testing for. An other thing is that asynchronous method that use async/ await should be named with the prefix "Async" or "TaskAsync.

    That said your test would be better written like this:

    [Test]
    public async void MicrosoftCom_Via_HttpClient_Modern_does_not_return_null ()
    {
        var bytes = await MicrosoftCom_Via_HttpClient_Impl_TaskAsync();
    
    Assert.IsNotNull (bytes);
        Assert.IsTrue (bytes.Length > 0);
    }
    
    [Test]
    public async void MicrosoftCom_Via_HttpClient_Modern_returns_a_byte_array_greater_zerro ()
    {
        var bytes = await MicrosoftCom_Via_HttpClient_Impl_TaskAsync();
    
        Assert.IsTrue (bytes.Length > 0);
    }
    
    private async Task<byte[]> MicrosoftCom_Via_HttpClient_Impl_TaskAsync ()
    {
        using (HttpClient client = new HttpClient ()) 
        {
            byte[] bytes = await client.GetByteArrayAsync("http://microsoft.com");
            return bytes;
        }
    }
    

    Hope that helps a little bit.

Sign In or Register to comment.