<commit_before>
using System;
using System.IO;
using NUnit.Framework;
using Mono.Addins;
using SimpleApp;
using System.Threading;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;

namespace UnitTests
{
	[TestFixture ()]
	public class TestMultiThreading: TestBase
	{
		public override void Setup ()
		{
			base.Setup ();
			GlobalInfoCondition.Value = "res";
		}

		[Test]
		public void ChildConditionAttributeMultithread ()
		{
			int threads = 50;
			using var testData = new TestData(threads);

			GlobalInfoCondition.Value = "";

			// Threads check the number of nodes in /SimpleApp/ConditionedWriters, which changes
			// from 0 to 1 as the GlobalInfoCondition condition changes

			testData.StartThreads (RunChildConditionAttributeTest);

			for (int n = 0; n < 20; n++) {
				Console.WriteLine ("Step " + n);
				testData.CheckCounters (0, 10000);

				GlobalInfoCondition.Value = "foo";

				testData.CheckCounters (1, 10000);

				GlobalInfoCondition.Value = "";
			}
		}

		void RunChildConditionAttributeTest (int index, TestData data)
		{
			while (!data.Stopped) {
				var writers = AddinManager.GetExtensionObjects<IWriter> ("/SimpleApp/ConditionedWriters");
				data.Counters [index] = writers.Length;
			}
		}

		[Test]
		public void EnableDisableMultithread ()
		{
			int threads = 50;
			int steps = 20;

			using var testData = new TestData (threads);

			testData.StartThreads ((index, data) => {
				while (!data.Stopped) {
					LoadAll(AddinManager.GetExtensionNodes<ItemSetNode>("/SimpleApp/ItemTree"));
					var writers = AddinManager.GetExtensionObjects<IWriter> ("/SimpleApp/Writers");
					testData.Counters [index] = writers.Length;
				}
			});

			for (int n = 0; n < steps; n++) {
				Console.WriteLine ("Step " + n);

				testData.CheckCounters (4, 10000);

				var ainfo1 = AddinManager.Registry.GetAddin ("SimpleApp.HelloWorldExtension");
				ainfo1.Enabled = false;

				testData.CheckCounters (3, 10000);

				var ainfo2 = AddinManager.Registry.GetAddin ("SimpleApp.FileContentExtension");
				ainfo2.Enabled = false;

				testData.CheckCounters (2, 10000);

				ainfo1.Enabled = true;
				ainfo2.Enabled = true;
			}
		}

		void LoadAll(IEnumerable<ExtensionNode> nodes)
		{
			foreach (var n in nodes.OfType<ItemSetNode>())
				LoadAll(n.GetChildNodes());
		}

		[Test]
		public void EventsMultithread()
		{
			int threads = 50;

			int totalAdded = 0;<commit_msg>Try to fix flaky test<commit_after>
using System;
using System.IO;
using NUnit.Framework;
using Mono.Addins;
using SimpleApp;
using System.Threading;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;

namespace UnitTests
{
	[TestFixture ()]
	public class TestMultiThreading: TestBase
	{
		public override void Setup ()
		{
			base.Setup ();
			GlobalInfoCondition.Value = "res";
		}

		[Test]
		public void ChildConditionAttributeMultithread ()
		{
			int threads = 50;
			using var testData = new TestData(threads);

			GlobalInfoCondition.Value = "";

			// Threads check the number of nodes in /SimpleApp/ConditionedWriters, which changes
			// from 0 to 1 as the GlobalInfoCondition condition changes

			testData.StartThreads (RunChildConditionAttributeTest);

			for (int n = 0; n < 20; n++) {
				Console.WriteLine ("Step " + n);
				testData.CheckCounters (0, 10000);

				GlobalInfoCondition.Value = "foo";

				testData.CheckCounters (1, 10000);

				GlobalInfoCondition.Value = "";
			}
		}

		void RunChildConditionAttributeTest (int index, TestData data)
		{
			while (!data.Stopped) {
				var writers = AddinManager.GetExtensionObjects<IWriter> ("/SimpleApp/ConditionedWriters");
				data.Counters [index] = writers.Length;
			}
		}

		[Test]
		public void EnableDisableMultithread ()
		{
			int threads = 50;
			int steps = 20;

			using var testData = new TestData (threads);

			testData.StartThreads ((index, data) => {
				while (!data.Stopped) {
					LoadAll(AddinManager.GetExtensionNodes<ItemSetNode>("/SimpleApp/ItemTree"));
					var writers = AddinManager.GetExtensionObjects<IWriter> ("/SimpleApp/Writers");
					testData.Counters [index] = writers.Length;
				}
				testData.Counters[index] = 0;
			});

			for (int n = 0; n < steps; n++) {
				Console.WriteLine ("Step " + n);

				testData.CheckCounters (4, 10000);

				var ainfo1 = AddinManager.Registry.GetAddin ("SimpleApp.HelloWorldExtension");
				ainfo1.Enabled = false;

				testData.CheckCounters (3, 10000);

				var ainfo2 = AddinManager.Registry.GetAddin ("SimpleApp.FileContentExtension");
				ainfo2.Enabled = false;

				testData.CheckCounters (2, 10000);

				ainfo1.Enabled = true;
				ainfo2.Enabled = true;
			}

			testData.Stopped = true;
			testData.CheckCounters(0, 10000);
		}

		void LoadAll(IEnumerable<ExtensionNode> nodes)
		{
			foreach (var n in nodes.OfType<ItemSetNode>())
				LoadAll(n.GetChildNodes());
		}

		[Test]
		public void EventsMultithread()
		{
			int threads = 50;

			int totalAdded = 0;
{'fraction_non_alphanumeric': 0.1164521319388576, 'fraction_numerical': 0.021922767497988736, 'mean_word_length': 3.6003700277520814, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '1348701', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1554, 'n_words': 377}