<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ActorInterface;
using JetBrains.Annotations;
using NLog;

namespace ActorTestingFramework
{
    public class TestingActorRuntime : IActorRuntime, ITestingRuntime
    {
        private static Logger LOGGER = LogManager.GetCurrentClassLogger();

        private readonly Dictionary<int, ActorId> taskIdToActorId =
            new Dictionary<int, ActorId>();

        private int nextActorId;

        private readonly Dictionary<ActorId, ActorInfo> actors =
            new Dictionary<ActorId, ActorInfo>();

        private readonly List<ActorInfo> actorList = new List<ActorInfo>();

        private volatile bool terminated;

        private readonly IScheduler scheduler;

        public TestingActorRuntime(IScheduler scheduler)
        {
            this.scheduler = scheduler;
        }

        #region Implementation of IActorRuntime

        public void RegisterMainTask(Task mainTask)
        {
            CreateActor(
                mainTask,
                null,
                "MainTask");
        }

        public Task StartMain(Action action)
        {
            Task task = new Task(action);
            RegisterMainTask(task);
            task.Start();
            return task;
        }

        public Task<T> StartMain<T>(Func<T> func)
        {
            Task<T> task = new Task<T>(func);
            RegisterMainTask(task);
            task.Start();
            return task;
        }

        public void TaskQueued(Task task, Action action)
        {
            CheckTerminated(OpType.CREATE);

            if (!taskIdToActorId.ContainsKey(task.Id))
            {
                // Task was created externally.
                LOGGER.Trace($"TaskQueued {task.Id}");

                Schedule(OpType.CREATE);

                var actorInfo = CreateActor(task, null);
                var oldAction = action;
                action = delegate
                {<commit_msg>Added allowTaskQueue field ...to TestingActorRuntime which allows users to prevents tasks being created outside the runtime.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ActorInterface;
using JetBrains.Annotations;
using NLog;

namespace ActorTestingFramework
{
    public class TestingActorRuntime : IActorRuntime, ITestingRuntime
    {
        private static Logger LOGGER = LogManager.GetCurrentClassLogger();

        private readonly Dictionary<int, ActorId> taskIdToActorId =
            new Dictionary<int, ActorId>();

        private int nextActorId;

        private readonly Dictionary<ActorId, ActorInfo> actors =
            new Dictionary<ActorId, ActorInfo>();

        private readonly List<ActorInfo> actorList = new List<ActorInfo>();

        private volatile bool terminated;

        private readonly IScheduler scheduler;

        public bool allowTaskQueue = true;

        public TestingActorRuntime(IScheduler scheduler)
        {
            this.scheduler = scheduler;
        }

        #region Implementation of IActorRuntime

        public void RegisterMainTask(Task mainTask)
        {
            CreateActor(
                mainTask,
                null,
                "MainTask");
        }

        public Task StartMain(Action action)
        {
            Task task = new Task(action);
            RegisterMainTask(task);
            task.Start();
            return task;
        }

        public Task<T> StartMain<T>(Func<T> func)
        {
            Task<T> task = new Task<T>(func);
            RegisterMainTask(task);
            task.Start();
            return task;
        }

        public void TaskQueued(Task task, Action action)
        {
            CheckTerminated(OpType.CREATE);

            if (!taskIdToActorId.ContainsKey(task.Id))
            {
                // Task was created externally.
                LOGGER.Trace($"TaskQueued {task.Id}");

                if (!allowTaskQueue)
                {
                    throw new InvalidOperationException("A Task was queued outside of the testing runtime!");
                }

                Schedule(OpType.CREATE);

                var actorInfo = CreateActor(task, null);
                var oldAction = action;
                action = delegate
                {
{'fraction_non_alphanumeric': 0.07695739460182913, 'fraction_numerical': 0.0, 'mean_word_length': 1.9003880983182406, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '123179', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1059, 'n_words': 290}