Small Task quiz knowledge

The task and the await /async never ceases to amaze me. That because there is so much under covers( see https://channel9.msdn.com/Events/TechDays/Techdays-2014-the-Netherlands/Async-programming-deep-dive )

Let’s say that we have this code that awaits and throws ( or not ) an exception:


        static async Task<bool> test(int delaySeconds, Exception throwEx)
        {
            Console.WriteLine($"enter {delaySeconds} ");
            await Task.Delay(delaySeconds * 1000);
            Console.WriteLine($"finish waiting {delaySeconds} and throw Exception:  {throwEx.Message}");

            if (throwEx != null)
            {
                throw throwEx;//never do this in production
            }
            return true;
        }

First problem:  Error order

What this code will produce as output for the line Console.WriteLine(“err ” + ex.GetType().Name);  ?


            Console.WriteLine("start");
            var t10Ex = test(delaySeconds: 10, throwEx: new ArgumentException("problem with argument"));
            var t19Ex = test(delaySeconds: 19, throwEx: new DllNotFoundException("not found dll"));
            var t100NotEx = test(delaySeconds: 100, throwEx: null);

            try
            {
                await Task.WhenAll(new Task[] { t19Ex, t10Ex, t100NotEx });
            
            }
            catch (Exception ex)
            {
                Console.WriteLine("err " + ex.GetType().Name);

            }
            finally
            {
                Console.WriteLine("in finally");

            }
            Console.WriteLine("waiting in main");
            await Task.Delay(100 * 1000);
            Console.WriteLine("finish");

Answer: err DllNotFoundException
. That is because the await it produce error for the first  item in the array  , not for the first item in chronological order. More, it will wait for all tasks to finish.

Second problem: Grab all  errors

How do you modify the above code to grab all errors ?
Answer: By introducing a new variable, tAll . Attention: the exception is the same ( dllNotFound), however, the whole AggregateException is in tAll.Exception

            Task tAll=null;
            try
            {
                tAll = Task.WhenAll(new Task[] { t19Ex, t10Ex, t100NotEx });
                await tAll;
            
            }
            catch (Exception ex)
            {
                Console.WriteLine("err " + ex.GetType().Name);
                Console.WriteLine(" all exception here:" + tAll.Exception);

            }
            finally
            {
                Console.WriteLine("in finally");

            }

Third problem: Continue With
Let’s modify slightly the code for using ContinueWith. When the “in continue with ” will be displayed ?


            Console.WriteLine("start");
            var t10Ex = test(delaySeconds: 10, throwEx: new ArgumentException("problem with argument"));
            var t20NotEx = test(delaySeconds: 20, throwEx: null);
            var t10ContinueWith = t10Ex.ContinueWith(async (previousTask) =>
            {
                await Task.Delay(25 * 1000);
                Console.WriteLine("in continue with " + previousTask.IsCompletedSuccessfully);

            }).Unwrap();
            try
            {
                await Task.WhenAll(t10Ex, t20NotEx );
                
            
            }
            catch (Exception ex)
            {
                Console.WriteLine("err " + ex.GetType().Name);

            }
            finally
            {
                Console.WriteLine("in finally");

            }
            Console.WriteLine("waiting in main");
            await Task.Delay(100 * 1000);
            Console.WriteLine("finish");

Answer: : AFTER displaying “waiting in main” . Basically, you do not wait for ContinueWith, you wait for the first task in the ContinueWith construct ( t10Ex in our case)

And a question for you : Why do we need Unwrap ? ( Hint: try the code without and see the exception!)

C# and Null object

There is a lot to talk i n programming that null is bad , for example https://www.yegor256.com/2014/05/13/why-null-is-bad.html   and https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer-science/ .

In C# we have a love-hate relationship with null :

Love : Because  the value type / struct cannot be null, C# creators invented Nullable<T> https://docs.microsoft.com/en-us/dotnet/api/system.nullable-1

Hate: Because  reference types / classes can be null, c# 8.0 invented nullable reference types : https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/nullable-reference-types – no doubt from me that will be in the language

I want to add my 2 cents here : Null is sometime good

1. For databases , a null field is different from a 0 field ( see this toilet paper null vs 0 as a reference : https://pbs.twimg.com/media/C7ojxbSVAAEX9Bt.jpg )

2. For finding if a item exists in an array of value types, how do you find that the value that you want to search exists or not with .FirstOrDefault ? If the array is composed of reference, you can compare with null ….

7 Steps from (Angular) WebApplication to Windows Store

I have deployed fairly easy the .NET Core Alphabet applcation to Windows Store . Supposing that you already have registered the name of the application to the windows store (https://partner.microsoft.com/en-us/dashboard/windows/overview )

Those are the steps:

1. Compile the Angular Application ( with ng build –prod –build-optimizer  )

2. Create a new WinJS App for Universal Windows

3. Copy the results ( index.html and js )from the point 1 to a dist folder

4. Save the files with BOM ( either from VS, Save as, then save with encoding, either using dos2unix –m  <namefile>

5. Modify the main.js by adding : window.location.href=’dist/index.html’;    

6. Modify the Visual Assets to the images of your application

7. Right click the application, Store => Create App Package . And deploy to the store

As a Proof of concept, see https://www.microsoft.com/en-us/p/netcorealphabet/9pdx9rv0xhds#activetab=pivot:overviewtab 

Also, all the source code it is at https://github.com/ignatandrei/netCoreAlphabet ( and you may want to contribute, right?)

C# integration testing in AzureDevOps with Docker containers– SqlServer and Cachet example

Every software that we make depends on others. For Stankins , as a general ETL data, it is more important to be tested with real data providers.For example, we may want to take data from Sql Server and send to Cachet . How can we have a SqlServer and a Cachet up and running easy ? The obvious answer our days is Docker.

Let’s see how a test for SqlServer looks

using FluentAssertions;
using Stankins.Alive;
using Stankins.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xbehave;
using Xunit;

namespace StankinsTestXUnit
{
    [Trait("ReceiverSqlServer", "")]
    [Trait("ExternalDependency","SqlServer")]
    public class TestReceiverSqlServer
    {
        [Scenario]
        [Example("Server=(local);Database=master;User Id=SA;Password = <YourStrong!Passw0rd>;")]
        public void TestReceiverDBServer(string connectionString)
        {
            IReceive status = null;
            IDataToSent data = null;
            $"Assume Sql Server instance {connectionString} exists , if not see docker folder".w(() => {

            });
            $"When I create the ReceiverDBServer ".w(() => status = new ReceiverDBSqlServer(connectionString));
            $"and receive data".w(async () =>
            {
                data = await status.TransformData(null);
            });
            $"the data should have a table".w(() =>
            {
                data.DataToBeSentFurther.Count.Should().Be(1);
            });
            $"and the result should be true".w(() =>
            {
                data.DataToBeSentFurther[0].Rows[0]["IsSuccess"].Should().Be(true);
            });


        }
    }
}

and for cachet :



using FluentAssertions;
using Stankins.FileOps;
using Stankins.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Stankins.Rest;
using Xbehave;
using Xunit;
using static System.Environment;
using Stankins.Trello;
using Stankins.Cachet;

namespace StankinsTestXUnit
{
    [Trait("Cachet", "")]
    [Trait("ExternalDependency", "Cachet")]
    public class TestSenderCachet
    {
        [Scenario]
        [Example("Assets/JSON/CachetV1Simple.txt", 3)]
        public void TestSimpleJSON(string fileName,int NumberRows)
        {
            IReceive receiver = null;
           
            IDataToSent data=null;
            var nl = Environment.NewLine;
            $"Given the file {fileName}".w(() =>
            {
                File.Exists(fileName).Should().BeTrue();
            });
            $"When I create the {nameof(ReceiveRest)} for the {fileName}".w(() => receiver = new ReceiveRestFromFile(fileName));
            $"And I read the data".w(async () =>data= await receiver.TransformData(null));
            $"Then should be a data".w(() => data.Should().NotBeNull());
            $"With a table".w(() =>
            {
                data.DataToBeSentFurther.Should().NotBeNull();
                data.DataToBeSentFurther.Count.Should().Be(1);
            });
            $"The number of rows should be {NumberRows}".w(() => data.DataToBeSentFurther[0].Rows.Count.Should().Be(NumberRows));
            $"and now I transform with {nameof(SenderCachet)}".w(async ()=>
                data=await new SenderCachet("http://localhost:8000","5DiHQgKbsJqck4TWhMVO").TransformData(data)
            );

        } 

    }
}

( I have use XBehave for extensions)

Nice and easy , right ? Not so!

For up and running SqlServer I have used a docker compose file

version: '3'
services:
   db:
     image: mcr.microsoft.com/mssql/server
     ports:
       - "1433:1433"
     environment:
       SA_PASSWORD: "<YourStrong!Passw0rd>"
       ACCEPT_EULA: "Y"
     healthcheck:
       test: sqlcmd -S (local) -U SA -P '<YourStrong!Passw0rd>' -Q 'select 1'

and in AzureDevOps yaml start the containers, run the tests, collect the code coverage, stop the containers

docker-compose -f stankinsv2/solution/StankinsV2/StankinsTestXUnit/Docker/docker-sqlserver-instance-linux.yaml up -d  
        

echo 'start regular test'
        
         dotnet build -c $(buildConfiguration) stankinsv2/solution/StankinsV2/StankinsV2.sln
        
         dotnet test stankinsv2/solution/StankinsV2/StankinsTestXUnit/StankinsTestXUnit.csproj --logger trx  --logger "console;verbosity=normal" --collect "Code coverage"
         echo 'coverlet'
         coverlet stankinsv2/solution/StankinsV2/StankinsTestXUnit/bin/$(buildConfiguration)/netcoreapp2.2/StankinsTestXUnit.dll --target "dotnet" --targetargs "test stankinsv2/solution/StankinsV2/StankinsTestXUnit/StankinsTestXUnit.csproj --configuration $(buildConfiguration) --no-build" --format opencover --exclude "[xunit*]*"
        
         echo 'compose down'
         docker-compose -f stankinsv2/solution/StankinsV2/StankinsTestXUnit/Docker/docker-sqlserver-instance-linux.yaml down
        

Easy, right ? That’s because SqlServer is well behaved and has a fully functional image on Docker

That is not so easy with Cachet . Cachet requires configuration – and more, after configuration, it generates a random token for write data  ( http://localhost:8000","5DiHQgKbsJqck4TWhMVO ) .

So it will be a task for docker to export the container and import again  - easy stuff, right ? Again, not.

So I start a small docker container with

docker run -p 8000:8000 –name myCachetContainer -e APP_KEY=base64:ybug5it9Koxwhfi5a6CORbWdpjVqXxkz/Tyj4K45GKc= -e DEBUG=false -e DB_DRIVER=sqlite cachethq/docker

and then browsing to http://localhost:8000 I have configured and grab the token

Now it is time to export :

docker export myCachetContainer -o cachet.tar

And to import as an image

docker import cachet.tar  mycac

And to run the image again

docker run -p 8000:8000  -e APP_KEY=base64:ybug5it9Koxwhfi5a6CORbWdpjVqXxkz/Tyj4K45GKc= -e DEBUG=false -e DB_DRIVER=sqlite cachethq/docker

And the image stopped! After many tries and docker inspect the initial image , I have resulted to

docker run -it -p 8000:8000 -e APP_KEY=base64:ybug5it9Koxwhfi5a6CORbWdpjVqXxkz/Tyj4K45GKc= -e DEBUG=false -e DB_DRIVER=sqlite --workdir /var/www/html --user 1001:1001 mycac "/sbin/entrypoint.sh"

So the workdir, user, and the entry point are not copied into the image and you should do youurself.

The final preparing for CI with Docker for Cachet ? I have docker push myimage to Docker Hub , and I will run it from docker compose.

So now my docker compose with sql server and cachet looks this way

version: '3'
services:
   db:
     image: mcr.microsoft.com/mssql/server
     ports:
       - "1433:1433"
     environment:
       SA_PASSWORD: "<YourStrong!Passw0rd>"
       ACCEPT_EULA: "Y"
     healthcheck:
       test: sqlcmd -S (local) -U SA -P '<YourStrong!Passw0rd>' -Q 'select 1'

  cachet:
     image: ignatandrei/ci_cachet
     ports:
       - "8000:8000"
      
     environment:
       APP_KEY: "base64:ybug5it9Koxwhfi5a6CORbWdpjVqXxkz/Tyj4K45GKc="
       DEBUG: "false"
       DB_DRIVER: "sqlite"
       
     user: "1001"   
     working_dir: "/var/www/html"
     entrypoint: "/sbin/entrypoint.sh"

And I have a nice C# integration tests with Azure Devops, Docker, Sql Server and Cachet ! You can see the code coverage report at https://codecov.io/gh/ignatandrei/stankins/src/master/stankinsv2/solution/StankinsV2/Stankins.Cachet/SenderCachet.cs

Andrei Ignat weekly software news(mostly .NET)

* indicates required

Please select all the ways you would like to hear from me:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.