Example #1
0
        public static Worker <Block, Block> CreateDestinationWorker(BinaryWriter writer, int blocksNumber, int blockLength,
                                                                    PackerMode packerMode, IGettableConveyer <Block> gettableConveyer, ILoggable logger)
        {
            IPuttableConveyer <Block> puttableConveyer;

            if (packerMode == PackerMode.Pack)
            {
                BinaryBlockWriter.WriteInfo(writer, blocksNumber, blockLength);
                puttableConveyer = new PutOnlyConveyer <Block>((Block block) => {
                    BinaryBlockWriter.WritePackedBlock(writer, block);
                });
            }
            else
            {
                puttableConveyer = new PutOnlyConveyer <Block>((Block block) => {
                    BinaryBlockWriter.WriteBlock(writer, block, blockLength);
                });
            }
            return(new Worker <Block, Block>("Destination", gettableConveyer, puttableConveyer, logger, block => block));
        }
Example #2
0
        public void CommonConveyerWorkersLogic_Test()
        {
            var commonSourceConveyer      = new LockableConveyer <SourceItem>();
            var commonDestinationConveyer = new LockableConveyer <DestinationItem>();

            byte[] sourceRawData = { 1, 2, 3, 4, 5 };

            int sourceRawDataIndex             = 0;
            List <SourceItem>      source      = new List <SourceItem>();
            List <DestinationItem> destination = new List <DestinationItem>();

            IGettableConveyer <SourceItem> gettableConveyer = new GetOnlyConveyer <SourceItem>((out bool stopped) => {
                stopped = sourceRawDataIndex == sourceRawData.Length;
                return(stopped ? null : new SourceItem()
                {
                    Data = sourceRawData[sourceRawDataIndex++]
                });
            });
            IPuttableConveyer <DestinationItem> puttableConveyer = new PutOnlyConveyer <DestinationItem>((DestinationItem item) => {
                destination.Add(item);
            });
            var workers = new List <IWorkable> {
                new Worker <SourceItem, SourceItem>("Source", gettableConveyer, commonSourceConveyer, null, item => item),
                new Worker <DestinationItem, DestinationItem>("Destination", commonDestinationConveyer, puttableConveyer, null, item => item)
            };

            for (int index = 1; index <= Environment.ProcessorCount; ++index)
            {
                workers.Add(new Worker <SourceItem, DestinationItem>($"Worker #{index}", commonSourceConveyer, commonDestinationConveyer, null, (SourceItem item) => {
                    return(new DestinationItem()
                    {
                        Data = item.Data
                    });
                }));
            }

            WorkerFactory.DoWork(workers, token);

            Assert.Equal(sourceRawData.Length, destination.Count);
        }