コード例 #1
0
        public void BasicProtectionRuntime()
        {
            var errors = null as IEnumerable <Diagnostic>;
            var node   = ConcurrentMock
                         .Build(@"
                    concurrent class VendingMachine 
                    { 
                        public    void coin();
                        protected void choc();
                        protected void toffee();

                        void main() 
                        {
                            for (;;)
                            {
                                coin >> (choc | toffee);
                            }
                        }
                    }", out errors);

            //must not have compilation errors
            Assert.IsNull(errors);

            var vm = node.Spawn("VendingMachine");

            ConcurrentMock.Fails(vm, "choc");
            ConcurrentMock.Fails(vm, "toffee");

            ConcurrentMock.Succeeds(vm, "coin", "choc");
            ConcurrentMock.Succeeds(vm, "coin", "toffee");

            node.Stop();
        }
コード例 #2
0
        public void BasicSingleton()
        {
            var errors = null as IEnumerable <Diagnostic>;
            var node   = ConcurrentMock
                         .Build(@"
                    concurrent object VendingMachine 
                    { 
                        public    void coin();
                        protected void choc();
                        protected void toffee();

                        void main() 
                        {
                            for (;;)
                            {
                                coin >> (choc | toffee);
                            }
                        }
                    }", out errors);

            //must not have compilation errors
            Assert.IsNull(errors);
            bool throws = false;

            try
            {
                var wrong = node.Spawn("VendingMachine");
            }
            catch
            {
                throws = true;
            }

            Assert.IsTrue(throws);

            var vm = node.Get("VendingMachine");

            ConcurrentMock.Fails(vm, "choc");
            ConcurrentMock.Fails(vm, "toffee");

            ConcurrentMock.Succeeds(vm, "coin", "choc");
            ConcurrentMock.Succeeds(vm, "coin", "toffee");

            node.Stop();
        }
コード例 #3
0
ファイル: ConcurrentDemos.cs プロジェクト: afrog33k/Excess
        public void DiningPhilosophers()
        {
            var errors = null as IEnumerable <Diagnostic>;
            var node   = ConcurrentMock
                         .Build(@"
                concurrent class philosopher 
                {
                    static int Meals = 10;

                    void main(string name, chopstick left, chopstick right) 
	                {
                        _name  = name;
	                    _left  = left;
	                    _right = right;
                               
                        for(int i = 0; i < Meals; i++)
                        {
	                        await think();
                        }    
	                }
	
	                void think()
	                {
                        console.write(_name + "" is thinking"");
                        seconds(rand(1.0, 2.0))
                            >> hungry();
	                }

	                void hungry()
	                {
                        console.write(_name + "" is hungry"");
	                    (_left.acquire(this) & _right.acquire(this)) 
                            >> eat();
	                }
	
	                void eat()
	                {
                        console.write(_name + "" is eating"");
                        await seconds(rand(1.0, 2.0));

                        _left.release(this); 
                        _right.release(this);
	                }
	                
                    private string _name;
                    private chopstick _left;
	                private chopstick _right;
                }

                concurrent class chopstick
                {
	                public void acquire(object owner)
                    {
                        if (_owner != null)
                        {
                            await release;
                        }
                        
                        _owner = owner;
                    }
	
	                public void release(object owner)
                    {
                        if (_owner != owner)
                            throw new InvalidOperationException();

                        _owner = null;
                    }

                    private object _owner;
                }", out errors, threads: 1);

            //must not have compilation errors
            Assert.IsNull(errors);

            var names = new[]
            {
                "Kant",
                "Archimedes",
                "Nietzche",
                "Plato",
                "Spinoza",
            };

            var chopsticks = names.Select(n => node
                                          .Spawn("chopstick"))
                             .ToArray();

            var phCount = names.Length;

            for (int i = 0; i < phCount; i++)
            {
                var left  = chopsticks[i];
                var right = i == phCount - 1 ? chopsticks[0] : chopsticks[i + 1];

                node.Spawn("philosopher", names[i], left, right);
            }

            Thread.Sleep(45000);
            node.Stop();
            node.WaitForCompletion();

            var items = console
                        .items()
                        .GroupBy(item =>
            {
                var key = 0;
                foreach (var name in names)
                {
                    if (item.Contains(name))
                    {
                        return(key);
                    }

                    key++;
                }

                Assert.IsTrue(false);
                return(-1);
            });
        }
コード例 #4
0
ファイル: ConcurrentDemos.cs プロジェクト: afrog33k/Excess
        public void Barbers()
        {
            var errors = null as IEnumerable <Diagnostic>;
            var node   = ConcurrentMock
                         .Build(@"
                concurrent class barbershop
                {
                    barber[] _barbers;
                    bool[] _busy;
                    
                    public barbershop(barber barber1, barber barber2)
                    {
                        _barbers = new [] {barber1, barber2}; 
                        _busy    = new [] {false, false}; 
                    }

                    public void visit(int client)
                    {
                        console.write($""Client: {client}, Barber1 {barber_status(0)},  Barber2: {barber_status(1)}"");
                        if (_busy[0] && _busy[1])
                            await visit.enqueue();

                        for(int i = 0; i < 2; i++)
                        {
                            if (!_busy[i]) 
                            {
                                await shave_client(client, i);
                                break;
                            }
                        }

                        visit.dequeue();
                    }

                    private void shave_client(int client, int which)
                    {
                        var barber = _barbers[which];
                        double tip = rand(5, 10);
                        
                        _busy[which] = true;

                        barber.shave(client)
                            >> barber.tip(client, tip);

                        _busy[which] = false;
                    }

                    private string barber_status(int which)
                    {
                        return _busy[which]
                            ? ""working""
                            : ""available"";
                    }
                }

                concurrent class barber
                {
                    int _index;
                    void main(int index)
                    {
                        _index = index;

                        while(true)
                            shave >> tip;
                    }

                    public void shave(int client)
                    {
                        await seconds(rand(1, 2));
                    }

                    double _tip = 0;
                    public void tip(int client, double amount)
                    {
                        _tip += amount;
                        console.write($""Barber {_index}: {client} tipped {amount:C2}, for a total of {_tip:C2}"");
                    }
                }", out errors, threads: 1);

            //must not have compilation errors
            Assert.IsNull(errors);

            var barber1 = node.Spawn("barber", 0);
            var barber2 = node.Spawn("barber", 1);
            var shop    = node.Spawn("barbershop", barber1, barber2);

            var rand    = new Random();
            var clients = 30;

            for (int i = 1; i <= clients; i++)
            {
                Thread.Sleep((int)(3000 * rand.NextDouble()));
                ConcurrentMock
                .SendAsync(shop, "visit", i);
            }

            Thread.Sleep(3000); //wait for last one
            node.Stop();
            node.WaitForCompletion();

            var output = console.items();

            Assert.AreEqual(output.Length, 60);
        }
コード例 #5
0
        public void ChameneosRedux()
        {
            IEnumerable <Diagnostic> errors;

            var node = ConcurrentMock
                       .Build(@"
                concurrent class Chameneo
                {
                    public enum Color
                    {
                        blue,
                        red,    
                        yellow,    
                    }

                    public Color Colour {get; private set;}
                    public int Meetings {get; private set;}
                    public int MeetingsWithSelf {get; private set;}
                    public Broker MeetingPlace {get; private set;}

                    public Chameneo(Broker meetingPlace, int color)
                    : this(meetingPlace, (Color)color)
                    {
                    }

                    public Chameneo(Broker meetingPlace, Color color)
                    {
                        MeetingPlace = meetingPlace;
                        Colour = color;
                        Meetings = 0;
                        MeetingsWithSelf = 0;
                    }
    
                    void main() 
	                {
                        for(;;)
                        {
                            MeetingPlace.request(this);
                            await meet;
                        }
	                }
	                
                    public void meet(Chameneo other, Color color)
                    {
                        Colour = compliment(Colour, color);
                        Meetings++;
                        if (other == this)
                            MeetingsWithSelf++;
                    }                    

                    public void print()
                    {
                        console.write($""{Colour}, {Meetings}, {MeetingsWithSelf}"");
                    }                    

                    private static Color compliment(Color c1, Color c2)
                    {
                        switch (c1)
                        {
                            case Color.blue:
                                switch (c2)
                                {
                                    case Color.blue: return Color.blue;
                                    case Color.red: return Color.yellow;
                                    case Color.yellow: return Color.red;
                                    default: break;
                                }
                                break;
                            case Color.red:
                                switch (c2)
                                {
                                    case Color.blue: return Color.yellow;
                                    case Color.red: return Color.red;
                                    case Color.yellow: return Color.blue;
                                    default: break;
                                }
                                break;
                            case Color.yellow:
                                switch (c2)
                                {
                                    case Color.blue: return Color.red;
                                    case Color.red: return Color.blue;
                                    case Color.yellow: return Color.yellow;
                                    default: break;
                                }
                                break;
                        }
                        throw new Exception();
                    }

                }

                concurrent class Broker
                {
                    int _meetings = 0;
                    public Broker(int meetings)
                    {
                        _meetings = meetings;
                    }

                    Chameneo _first = null;
                    public void request(Chameneo creature)
                    {
                        if (_first != null)
                        {
                            //perform meeting
                            var firstColor = _first.Colour;
                            _first.meet(creature, creature.Colour);
                            creature.meet(_first, firstColor);
                            
                            //prepare for next
                            _first = null;
                            _meetings--;
                            if (_meetings == 0)
                                Node.Stop();
                        }
                        else
                            _first = creature;
                    }
                }", out errors, threads: 6);

            //must not have compilation errors
            Assert.IsNull(errors);

            //run for a couple different sets
            const int blue       = 0;
            const int red        = 1;
            const int yellow     = 2;
            const int iterations = 6 * 1000 * 1000;

            Action <int, int[]> run = (meeetings, colors) =>
            {
                var broker = node.Spawn("Broker", meeetings);
                for (int i = 0; i < colors.Length; i++)
                {
                    node.Spawn("Chameneo", broker, colors[i]);
                }
            };

            Stopwatch sw = new Stopwatch();

            sw.Start();
            {
                node.StopCount(2);

                run(iterations, new[] { blue, red, yellow });
                //node.WaitForCompletion();
                //node.Restart();
                run(iterations, new[] { blue, red, yellow, red, yellow, blue, red, yellow, red, blue });

                node.WaitForCompletion();
            }
            sw.Stop();

            TimeSpan rt = TimeSpan.FromTicks(sw.ElapsedTicks);
            var      ts = rt.TotalSeconds.ToString();

            Assert.IsNotNull(ts);
        }
コード例 #6
0
        public void ThreadRing()
        {
            var errors = null as IEnumerable <Diagnostic>;
            var node   = ConcurrentMock
                         .Build(@"
                concurrent class ring_item
                {
                    int _idx;
                    public ring_item(int idx)
                    {
                        _idx = idx;
                    }
                    
                    public ring_item Next {get; set;}

                    static int ITERATIONS = 50*1000*1000;
                    public void token(int value)
                    {
                        if (value >= ITERATIONS)
                        {
                            console.write(_idx);
                            Node.Stop();
                        }
                        else
                            Next.token(value + 1);
                    }                    
                }", out errors, threads: 1);

            //must not have compilation errors
            Assert.IsNull(errors);

            const int ringCount = 503;

            var items = new ConcurrentObject[ringCount];

            for (int i = 0; i < ringCount; i++)
            {
                items[i] = node.Spawn("ring_item", i);
            }

            //update connectivity
            for (int i = 0; i < ringCount; i++)
            {
                var curr = items[i];
                var next = i < ringCount - 1 ? items[i + 1] : items[0];
                ConcurrentMock.Send(curr, "Next", next);
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();
            {
                //run it by sending the first token, it will go around 50M times
                ConcurrentMock.Send(items[0], "token", 0);
                node.WaitForCompletion();
            }
            sw.Stop();

            TimeSpan rt = TimeSpan.FromTicks(sw.ElapsedTicks);
            var      ts = rt.TotalSeconds.ToString();

            Assert.IsNotNull(ts);
        }