aboutsummaryrefslogtreecommitdiff
path: root/ClojureCLR/Clojure/Clojure.Tests/LibTests/LockingTransactionTests.cs
blob: 2368255913206d5d6a59023cf3330aba2049e746 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
 *   Copyright (c) David Miller. All rights reserved.
 *   The use and distribution terms for this software are covered by the
 *   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 *   which can be found in the file epl-v10.html at the root of this distribution.
 *   By using this software in any fashion, you are agreeing to be bound by
 * 	 the terms of this license.
 *   You must not remove this notice, or any other, from this software.
 **/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using NUnit.Framework;
using Rhino.Mocks;

using clojure.lang;

using RMExpect = Rhino.Mocks.Expect;
using System.Threading;

namespace Clojure.Tests.LibTests
{
    // TODO: Add tests for LockingTransaction
    [TestFixture]
    public class LockingTransactionTests : AssertionHelper
    {


        //// TODO: Make this work.

        //// This test is taken from the Java code.
        //[Test]
        //public void BigTest()
        //{
        //    // We have to start the main work unit on its own thread because
        //    // the main during testing is STA and so does not support waiting on multiple handles.

        //    BigTester<Incrementer> tester = new BigTester<Incrementer>(5, 10, 2000);
        //    EventWaitHandle h = new EventWaitHandle(false, EventResetMode.ManualReset);
        //    Thread t = new Thread(tester.Work);
        //    t.Start(h);
        //    h.WaitOne();
        //    Console.WriteLine("Done");
        //}

        //class BigTester<T> where T : LockingTransactionTests.RefTester
        //{
        //    int _nthreads;
        //    int _niters;
        //    int _nitems;

        //    public BigTester(int nthreads, int nitems, int niters)
        //    {
        //        _nthreads = nthreads;
        //        _nitems = nitems;
        //        _niters = niters;
        //    }

        //    public void Work(object o)
        //    {
        //        List<Ref> refs = new List<Ref>();

        //        for (int i = 0; i < _nitems; i++)
        //            refs.Add(new Ref(0));

        //        List<Thread> threads = new List<Thread>(_nthreads);
        //        List<EventWaitHandle> handles = new List<EventWaitHandle>(_nthreads);
        //        List<Incrementer> tasks = new List<Incrementer>(_nthreads);

        //        for (int i = 0; i < _nthreads; i++)
        //        {
        //            List<Ref> copy = refs.GetRange(0, refs.Count);
        //            Shuffle(copy);
        //            tasks.Add(new Incrementer(i, _niters, copy));
        //            threads.Add(new Thread(tasks[i].Work));
        //            handles.Add(new EventWaitHandle(false, EventResetMode.ManualReset));
        //        }


        //        for (int i = 0; i < _nthreads; i++)
        //        {
        //            threads[i].Name = "Thr " + i;
        //            threads[i].Start(handles[i]);
        //        }

        //        EventWaitHandle.WaitAll(handles.ToArray());

        //        foreach (Incrementer task in tasks)
        //            Console.WriteLine("Task {0}: {1} millisecs", task.Id, task.Nanos / 10000.0);

        //        foreach (Ref r in refs)
        //            Console.WriteLine("Ref is {0}", r.get());

        //        EventWaitHandle ewh = (EventWaitHandle)o;
        //        ewh.Set();

        //    }


        //    void Shuffle(List<Ref> refs)
        //    {
        //    }
        //}

        //public abstract class RefTester
        //{
        //    readonly int _id;
        //    public int Id
        //    {
        //      get { return _id; }  
        //    } 

        //    protected readonly int _niters;
        //    protected readonly List<Ref> _items;

        //    long _nanos = 0;
        //    public long Nanos
        //    {
        //      get { return _nanos; }
        //    }


        //    public RefTester(int id, int niters, List<Ref> items)
        //    {
        //        _id = id;
        //        _niters = niters;
        //        _items = items;
        //    }
            
        //    public void Work(object o)
        //    {
        //        for (int i = 0; i < _niters; i++)
        //        {
        //            long startTime = DateTime.Now.Ticks;
        //            LockingTransaction.runInTransaction(this.TxUnit);
        //            long finishTime = DateTime.Now.Ticks;
        //            _nanos += finishTime - startTime;
        //        }

        //        EventWaitHandle h = (EventWaitHandle)o;
        //        h.Set();
        //    }

        //    public abstract object TxUnit(object[] args);
        //}

        //class Incrementer : RefTester
        //{
        //    public Incrementer(int id, int niters, List<Ref> items)
        //        : base(id, niters, items)
        //    {
        //    }

        //    public override object TxUnit(object[] args)
        //    {
        //        foreach (Ref r in _items)
        //        {
        //            int val = (int)r.get();
        //            r.set(val + 1);
        //        }
        //        return null;
        //    }
        //}


        //class Commuter : RefTester
        //{
        //    public Commuter(int id, int niters, List<Ref> items)
        //        : base(id, niters, items)
        //    {
        //    }

        //    public override object TxUnit(object[] args)
        //    {
        //        foreach (Ref r in _items)
        //        {
        //            r.commute(Commuter.Incr,null);
        //        }
        //        return null;
        //    }

        //    static object Incr(object[] args)
        //    {
        //        int val = (int)args[0];
        //        return val + 1;
        //    }
        //}

    }
}