Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / bug-415577.cs
1 using System;
2 using System.Threading;
3 using System.Runtime.Remoting;
4 using System.Runtime.Remoting.Activation;
5 using System.Runtime.Remoting.Contexts;
6 using System.Runtime.Remoting.Messaging;
7
8 public class MyContextAttribute: Attribute, IContextAttribute  {
9         public void GetPropertiesForNewContext (IConstructionCallMessage msg)
10         {
11         }
12
13         public bool IsContextOK (Context ctx, IConstructionCallMessage msg)
14         {
15                 return false;
16         }
17 }
18
19 // CBO class whose objects are always in the correct context
20 class UnlockedCbo : ContextBoundObject {
21         public int Counter;
22
23
24         void Inc (ref int a)
25         {
26                 a++;
27         }
28
29         public void Inc ()
30         {
31                 Inc (ref Counter);
32         }
33 }
34
35 // CBO class whose objects are always out of context
36 [MyContext]
37 class LockedCbo : UnlockedCbo {
38 }
39
40 class Mbr : MarshalByRefObject {
41         public int Counter;
42
43         void Inc (ref int a)
44         {
45                 a++;
46         }
47
48         public void Inc ()
49         {
50                 Inc (ref Counter);
51         }
52 }
53
54 class Test {
55         static int Main ()
56         {
57                 // warning CS0197 is expected several times
58                 
59                 UnlockedCbo uc = new UnlockedCbo ();
60                 Interlocked.Increment (ref uc.Counter);
61                 uc.Inc ();
62
63                 LockedCbo lc = new LockedCbo ();
64                 try {
65                         Interlocked.Increment (ref lc.Counter);
66                         return 1;
67                 } catch (InvalidOperationException) {
68                 }
69
70                 lc.Inc ();
71
72                 if (lc.Counter != 1)
73                         return 2;
74
75                 Mbr m = new Mbr ();
76                 Interlocked.Increment (ref m.Counter);
77                 m.Inc ();
78
79                 if (m.Counter != 2)
80                         return 3;
81
82                 Mbr rm = (Mbr) CreateRemote (typeof (Mbr));
83                 try {
84                         Interlocked.Increment (ref rm.Counter);
85                         return 4;
86                 } catch (InvalidOperationException) {
87                 }
88
89                 rm.Inc ();
90
91                 if (rm.Counter != 1)
92                         return 5;
93
94                 return 0;
95         }
96
97         static object CreateRemote (Type t)
98         {
99                 AppDomain d = AppDomain.CreateDomain ("foo");
100                 return d.CreateInstanceAndUnwrap (t.Assembly.FullName, t.FullName);
101         }
102 }