Basit Bir Test Framework Gelistirelim 15

2023-05-11

Testlerin belirli bir süre sınırlamasına tabi olması önemlidir. Bazı testlerin beklenenden daha uzun sürmesi veya sonsuz bir döngüye girmesi durumunda, süre sınırlaması olan bir test, bu tür sorunları belirlemeyi ve test sürecini düzgün bir şekilde yönetmeyi kolaylaştırır.

Her test için bir süre sınırlaması ayarlamak için TestRunner‘ı güncelleyelim:

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Diagnostics;

// ...

public static class TestRunner
{
    // ...

    private static async Task<(bool, string)> RunTestMethodAsync<T>(T testClassInstance, MethodInfo testMethod, object[] parameters = null)
    {
        try
        {
            var stopwatch = Stopwatch.StartNew();
            
            var result = testMethod.Invoke(testClassInstance, parameters);
            if (result is Task task)
            {
                if (await Task.WhenAny(task, Task.Delay(TimeSpan.FromSeconds(5))) == task)
                {
                    // task completed within timeout
                    await task; // just in case the task is not yet fully complete
                }
                else
                {
                    // task did not complete within timeout
                    throw new TimeoutException($"Test '{testMethod.Name}' did not complete within the 5 seconds time limit.");
                }
            }

            stopwatch.Stop();

            var message = $"Test '{testMethod.Name}' completed in {stopwatch.ElapsedMilliseconds} ms";
            Console.WriteLine(message);

            // Test başarılı
            return (true, message);
        }
        catch (Exception ex)
        {
            // ... önceki kod ...

            var message = $"Test '{testMethod.Name}' failed: {ex.Message}";
            Console.WriteLine(message);

            return (false, message);
        }
    }

    // ...
}

Bu kod örneğinde, RunTestMethodAsync metodu Task.WhenAny metodu kullanarak belirli bir süre sınırlaması (bu örnekte 5 saniye) ile asenkron bir test görevini bekler. Eğer test görevi zaman aşımına uğrarsa, bir TimeoutException oluşturulur ve test başarısız kabul edilir. Bu özellik, testlerin beklenenden daha uzun sürdüğü veya sonsuz döngülere girdiği durumları belirlemeyi ve yönetmeyi kolaylaştırır.

Eğer test metotları asenkron değilse, bu yaklaşım işlemeyecektir. Bu durumda, test metotlarının belirli bir süre limiti içerisinde tamamlanmasını garanti etmek için daha karmaşık bir yaklaşım gerekebilir.



More posts like this

The SemaphoreSlim Class: Multitask-Based Programming in C#

2023-06-11 | #net #semaphoreslim

Overview The SemaphoreSlim class is a structure used in C# to control one or more threads using a specific resource or operation concurrently. SemaphoreSlim limits the number of threads that can access the resource at the same time. The use of SemaphoreSlim is often used to prevent deadlock situations in multi-threaded applications and to ensure that a specific resource is used by only one or more threads at the same time.

Continue reading 


LINQ and Optimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #optimistic-concurrency

When working with databases, it is of great importance to properly manage concurrent operations and ensure data integrity. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to ensure data integrity using LINQ (Language Integrated Query) with optimistic concurrency. What is Optimistic Concurrency? Optimistic concurrency is a method where resources are not locked when a transaction begins, but changes are checked before the transaction is completed.

Continue reading 


LINQ and Pessimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #pessimistic-concurrency

When working with databases, managing concurrent operations and ensuring data integrity are of great importance. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to use LINQ (Language Integrated Query) with pessimistic concurrency to maintain data integrity. What is Pessimistic Concurrency? Pessimistic concurrency is based on the principle of locking the relevant data when a transaction begins and maintaining the lock until the transaction is completed.

Continue reading 