Exemple #1
0
        public void EnqueueTest_SuccessfullyAdding()
        {
            //arrange
            var producer = new ProducerConsumer <Block>();
            var block    = new Block(0, null);

            //act
            producer.Enqueue(block);
            producer.Enqueue(block);
            var resultIsEmpty = producer.IsEmpty;

            //assert
            Assert.AreEqual(resultIsEmpty, false);
        }
Exemple #2
0
        public void EnqueueTest_AddNull()
        {
            //arrange
            var producer = new ProducerConsumer <Block>();

            //act

            //assert
            Assert.ThrowsException <ArgumentNullException>(() => producer.Enqueue(null));
        }
Exemple #3
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Enter exit to exit!");
            var cl = Console.ReadLine();

            do
            {
                ProtoQueue.Enqueue(cl);
                cl = Console.ReadLine();
            } while (cl != "exit");
        }
Exemple #4
0
        public void EnqueueTest_WhenQueueStopped()
        {
            //arrange
            var producer = new ProducerConsumer <Block>();
            var block    = new Block(0, null);

            //act
            producer.Stop();

            //assert
            Assert.ThrowsException <InvalidOperationException>(() => producer.Enqueue(block));
        }
Exemple #5
0
        public void DequeueTest_DequeueItemFromNotEmptyQueue()
        {
            //arrange
            var producer = new ProducerConsumer <Block>();
            var block    = new Block(0, null);

            //act
            producer.Enqueue(block);
            var resultItemFromDequeue = producer.Dequeue();

            //assert
            Assert.IsNotNull(resultItemFromDequeue);
        }
 public int Run()
 {
     try
     {
         workingThreads.ForEach(thread => thread.Start());
         while (!interrupted)
         {
             if (tasks.Size() > 2 * concurrency || results.Size() > 2 * concurrency)
             {
                 Thread.Yield();
                 continue;
             }
             var data = getDataBlock();
             if (data == null)
             {
                 tasks.Stop();
                 break;
             }
             tasks.Enqueue(data);
         }
         // join only processing tread and not the result aggregator
         for (var i = 1; i < workingThreads.Count; i++)
         {
             workingThreads[i].Join();
         }
         // notifies the result aggregator thread that no new information would be added and wait for it to finish
         results.Stop();
         workingThreads[0].Join();
     } catch (HandledException e)
     {
         this.Interrupt();
     } catch (Exception e)
     {
         Console.WriteLine($"Unhandled exception: {e.Message}");
         this.Interrupt();
     }
     return(interrupted ? 1 : 0);
 }
Exemple #7
0
        public Task InitAsync()
        {
            if (!Directory.Exists(PluginPath))
            {
                Directory.CreateDirectory(PluginPath);
            }
            if (!Directory.Exists(ProtoPath))
            {
                Directory.CreateDirectory(ProtoPath);
            }
            return(Task.Run(() =>
            {
                var protoFiles = Directory.GetFiles(ProtoPath, "*.proto");
                foreach (var file in protoFiles)
                {
                    var NeedGenerate = true;
                    var GenerateDllPath = string.Empty;
                    var fileName = Path.GetFileNameWithoutExtension(file);
                    var csharp_out = Path.Combine(BaseDirectory, $"plugins/.{fileName}");
                    if (Directory.Exists(csharp_out))
                    {
                        var pluginYml = Path.Combine(csharp_out, $"plugin.yml");
                        GenerateDllPath = Path.Combine(csharp_out, $"{fileName}.dll");
                        var xmlDocPath = Path.Combine(csharp_out, $"{fileName}.xml");
                        if (File.Exists(pluginYml) && File.Exists(GenerateDllPath) && File.Exists(xmlDocPath))
                        {
                            var setting = new ProtoPluginModel();
                            var dic = new Dictionary <string, string>();

                            #region 文件读取

                            using (FileStream fs = new FileStream(pluginYml, FileMode.Open, FileAccess.Read))
                            {
                                using (StreamReader read = new StreamReader(fs, Encoding.Default))
                                {
                                    string strReadline;
                                    while ((strReadline = read.ReadLine()) != null)
                                    {
                                        var kv = strReadline.Split(':');
                                        if (kv.Length == 2)
                                        {
                                            dic.Add(kv[0].Trim(), kv[1].Trim());
                                        }
                                    }
                                }

                                dic.TryGetValue("FileName", out string fName);
                                setting.FileName = fName;
                                dic.TryGetValue("DllFileMd5", out string dName);
                                setting.DllFileMd5 = dName;
                                dic.TryGetValue("ProtoFileMd5", out string pName);
                                setting.ProtoFileMd5 = pName;
                                dic.TryGetValue("XmlFileMd5", out string xName);
                                setting.XmlFileMd5 = xName;
                            }

                            #endregion 文件读取

                            var protoMd5 = file.FileMd5();
                            var dllMd5 = GenerateDllPath.FileMd5();
                            var xmlMd5 = xmlDocPath.FileMd5();
                            if (setting.ProtoFileMd5 == protoMd5 && setting.DllFileMd5 == dllMd5 && setting.XmlFileMd5 == xmlMd5)
                            {
                                NeedGenerate = false;
                            }
                        }
                    }
                    if (NeedGenerate)
                    {
                        protoQueue.Enqueue(file);
                    }
                    else
                    {
                        pluginFactory.LoadAsync(GenerateDllPath);
                    }
                }
            }));
        }
        public static Task InitAsync()
        {
            if (!Directory.Exists(PluginPath))
            {
                Directory.CreateDirectory(PluginPath);
            }
            if (!Directory.Exists(ProtoPath))
            {
                Directory.CreateDirectory(ProtoPath);
            }
            Handers = new ConcurrentDictionary <string, GrpcMethodHandlerInfo>();
            return(Task.Factory.StartNew(() =>
            {
                var dllFiles = Directory.GetFiles(PluginPath, "*.dll");
                foreach (var file in dllFiles)
                {
                    DllQueue.Enqueue(file);
                }

                var protoFiles = Directory.GetFiles(ProtoPath, "*.proto");
                foreach (var file in protoFiles)
                {
                    var NeedGenerate = true;
                    var GenerateDllPath = string.Empty;
                    var fileName = Path.GetFileNameWithoutExtension(file);
                    var csharp_out = Path.Combine(BaseDirectory, $"plugins/.{fileName}");
                    if (Directory.Exists(csharp_out))
                    {
                        var pluginYml = Path.Combine(csharp_out, $"plugin.yml");
                        GenerateDllPath = Path.Combine(csharp_out, $"{fileName}.dll");
                        var xmlDocPath = Path.Combine(csharp_out, $"{fileName}.xml");
                        if (File.Exists(pluginYml) && File.Exists(GenerateDllPath) && File.Exists(xmlDocPath))
                        {
                            var deserializer = new DeserializerBuilder()
                                               .WithNamingConvention(new CamelCaseNamingConvention())
                                               .Build();
                            var setting = new ProtoPluginModel();
                            using (FileStream fs = new FileStream(pluginYml, FileMode.Open, FileAccess.Read))
                            {
                                var dic = (Dictionary <object, object>)deserializer.Deserialize(new StreamReader(fs, Encoding.Default));

                                dic.TryGetValue("FileName", out object fName);
                                setting.FileName = fName?.ToString();

                                dic.TryGetValue("DllFileMD5", out object dName);
                                setting.DllFileMD5 = dName?.ToString();

                                dic.TryGetValue("ProtoFileMD5", out object pName);
                                setting.ProtoFileMD5 = pName?.ToString();

                                dic.TryGetValue("XmlFileMD5", out object xName);
                                setting.XmlFileMD5 = xName?.ToString();
                                //var setting = deserializer.Deserialize<ProtoPluginModel>(File.ReadAllText(pluginYml));
                            }
                            var protoMD5 = file.GetMD5();
                            var dllMD5 = GenerateDllPath.GetMD5();
                            var xmlMD5 = xmlDocPath.GetMD5();
                            if (setting.ProtoFileMD5 == protoMD5 && setting.DllFileMD5 == dllMD5 && setting.XmlFileMD5 == xmlMD5)
                            {
                                NeedGenerate = false;
                            }
                        }
                    }
                    if (NeedGenerate)
                    {
                        ProtoQueue.Enqueue(file);
                    }
                    else
                    {
                        DllQueue.Enqueue(GenerateDllPath);
                    }
                }
            }));
        }