CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
BusyLockTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="BusyLockTests.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>no summary</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Text;
12using Csla.Threading;
13using UnitDriven;
14using Csla.Core;
15using System.ComponentModel;
16
17#if NUNIT
18using NUnit.Framework;
19using TestClass = NUnit.Framework.TestFixtureAttribute;
20using TestInitialize = NUnit.Framework.SetUpAttribute;
21using TestCleanup = NUnit.Framework.TearDownAttribute;
22using TestMethod = NUnit.Framework.TestAttribute;
23using TestSetup = NUnit.Framework.SetUpAttribute;
24#elif MSTEST
25using Microsoft.VisualStudio.TestTools.UnitTesting;
26#endif
27
29{
30 [TestClass]
31 public class BusyLockTests
32 {
33 private class BusyBO : INotifyBusy
34 {
35 private bool _busy;
36
37 public bool IsBusy
38 {
39 get { return _busy; }
40 }
41
42 public bool IsSelfBusy
43 {
44 get { return _busy; }
45 }
46
47 public void MarkBusy(bool busy)
48 {
49 _busy = busy;
50 if (BusyChanged != null)
51 BusyChanged(this, new BusyChangedEventArgs("", busy));
52 }
53
54 public event BusyChangedEventHandler BusyChanged;
55
56 public event EventHandler<ErrorEventArgs> UnhandledAsyncException;
57
58 protected virtual void OnUnhandledAsyncException()
59 {
60 if (UnhandledAsyncException != null)
61 UnhandledAsyncException(this, new ErrorEventArgs(null, null));
62 }
63 }
64
65 [TestMethod]
66 public void SimpleLock()
67 {
68 BusyBO busy = new BusyBO();
69 busy.MarkBusy(true);
70 System.ComponentModel.BackgroundWorker worker = new System.ComponentModel.BackgroundWorker();
71 worker.DoWork += (o, e) =>
72 {
73 System.Threading.Thread.Sleep(10);
74 busy.MarkBusy(false);
75 };
76 worker.RunWorkerAsync();
77
78 BusyLock.WaitOne(busy);
79 Assert.IsFalse(busy.IsBusy);
80 }
81
82 [TestMethod]
83 public void TestTimeout()
84 {
85 BusyBO busy = new BusyBO();
86 busy.MarkBusy(true);
87 BusyLock.WaitOne(busy, TimeSpan.FromMilliseconds(10));
88 Assert.IsTrue(busy.IsBusy);
89 }
90
91 [TestMethod]
92 public void TestBusyLockObject()
93 {
94 BusyBO busy = new BusyBO();
95 busy.MarkBusy(true);
96 using (BusyLocker bl = new BusyLocker(busy, TimeSpan.FromSeconds(1)))
97 {
98 System.ComponentModel.BackgroundWorker worker = new System.ComponentModel.BackgroundWorker();
99 worker.DoWork += (o, e) => busy.MarkBusy(false);
100 worker.RunWorkerAsync();
101 }
102 Assert.IsFalse(busy.IsBusy);
103 }
104 }
105}
Event arguments for the BusyChanged event.
Event arguments for an unhandled async exception.
Implementation of a lock that waits while a target object is busy.
Definition: BusyLock.cs:53
Interface defining an object that notifies when it is busy executing an asynchronous operation.
Definition: INotifyBusy.cs:17
delegate void BusyChangedEventHandler(object sender, BusyChangedEventArgs e)
Delegate for handling the BusyChanged event.