Statistics
| Branch: | Revision:

root / trunk / Pithos.Core.Test / IdleBatchTest.cs @ 79f92570

History | View | Annotate | Download (12.6 kB)

1
// -----------------------------------------------------------------------
2
// <copyright file="IdleBatchTest.cs" company="Microsoft">
3
// TODO: Update copyright text.
4
// </copyright>
5
// -----------------------------------------------------------------------
6

    
7
using System.Collections.Concurrent;
8
using System.ComponentModel;
9
using System.IO;
10
using System.Threading;
11
using System.Threading.Tasks;
12
using System.Threading.Tasks.Dataflow;
13
using NUnit.Framework;
14
using Pithos.Core.Agents;
15

    
16
namespace Pithos.Core.Test
17
{
18
    using System;
19
    using System.Collections.Generic;
20
    using System.Linq;
21
    using System.Text;
22

    
23
    /// <summary>
24
    /// TODO: Update summary.
25
    /// </summary>
26
    [TestFixture]
27
    public class IdleBatchTest
28
    {
29

    
30

    
31
        [Test]
32
        public void TestEnqueue()
33
        {
34
            var queue=new ConcurrentQueue<FileSystemEventArgs>();
35
            var batch = new IdleBatch<FileSystemEventArgs>(1000,queue.AddFromEnumerable );
36
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Created,"path1","file1"));
37
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
38
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
39
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
40
            batch.ForceTrigger();
41
            
42
            Assert.That(queue.Count,Is.EqualTo(4));
43
        }
44

    
45
        [Test]
46
        public void TestTimeout()
47
        {
48
            var queue=new ConcurrentQueue<FileSystemEventArgs>();
49
            var batch = new IdleBatch<FileSystemEventArgs>(100,queue.AddFromEnumerable );
50
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Created,"path1","file1"));
51
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
52
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
53
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
54
            Thread.Sleep(500);
55
            
56
            Assert.That(queue.Count,Is.EqualTo(4));
57
            queue.Clear();
58
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
59
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
60
            Thread.Sleep(500);
61
            
62
            Assert.That(queue.Count,Is.EqualTo(2));
63
        }
64

    
65
        [Test]
66
        public void TestCreationSequence()
67
        {
68
            Dictionary<string, FileSystemEventArgs[]> events = null;
69
            var batch = new FileEventIdleBatch(100, dict => events = dict);
70

    
71
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Created,"path1","file1"));
72
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
73
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
74
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
75
            Thread.Sleep(500);
76

    
77
            Assert.That(events[@"path1\file1"].Select(arg => arg.ChangeType), Is.EquivalentTo(new[] { WatcherChangeTypes.Created }));
78
        }
79

    
80
        [Test]
81
        public void TestCreateDeleteSequence()
82
        {
83
            Dictionary<string, FileSystemEventArgs[]> events = null;
84
            var batch = new FileEventIdleBatch(100, dict => events = dict);
85

    
86
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Created,"path1","file1"));
87
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
88
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
89
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Deleted, "path1", "file1"));
90
            Thread.Sleep(500);
91

    
92
            Assert.That(events.ContainsKey("path1\\file1"), Is.False); 
93
        }
94

    
95
        [Test]
96
        public void TestChangeSequence()
97
        {
98
            Dictionary<string, FileSystemEventArgs[]> events = null;
99
            var batch = new FileEventIdleBatch(100, dict => events = dict);
100
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
101
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
102
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
103
            Thread.Sleep(500);
104
            
105
            Assert.That(events[@"path1\file1"].Select(arg=>arg.ChangeType),Is.EquivalentTo(new[]{WatcherChangeTypes.Changed}));
106
        }
107
        
108
        [Test]
109
        public void TestEnsureEmptyAfterTrigger()
110
        {
111
            Dictionary<string, FileSystemEventArgs[]> events = null;
112
            var batch = new FileEventIdleBatch(100, dict => events = dict);
113
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
114
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
115
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
116
            Thread.Sleep(500);
117
            Assert.That(events[@"path1\file1"].Select(arg => arg.ChangeType), Is.EquivalentTo(new[] { WatcherChangeTypes.Changed }));
118
            events = new Dictionary<string, FileSystemEventArgs[]>();
119
            Thread.Sleep(500);
120
            Assert.That(events, Is.Empty);
121
        }
122
        
123
        [Test]
124
        public void TestChangeDeleteSequence()
125
        {
126
            Dictionary<string, FileSystemEventArgs[]> events = null;
127
            var batch = new FileEventIdleBatch(100, dict => events = dict);
128
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
129
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
130
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Deleted, "path1", "file1"));
131
            Thread.Sleep(500);
132

    
133
            Assert.That(events[@"path1\file1"].Select(arg => arg.ChangeType), Is.EquivalentTo(new[] { WatcherChangeTypes.Deleted }));
134
        }
135

    
136
        [Test]
137
        public void TestCreateChangeDeleteSequence()
138
        {
139
            Dictionary<string, FileSystemEventArgs[]> events = null;
140
            var batch = new FileEventIdleBatch(100, dict => events = dict);
141
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Created, "path1", "file1"));
142
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
143
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Deleted, "path1", "file1"));
144
            Thread.Sleep(500);
145

    
146
            Assert.That(events.ContainsKey("path1\\file1"), Is.False);            
147
        }
148

    
149
        [Test]
150
        public void TestCreateDeleteCreateSequence()
151
        {
152
            Dictionary<string, FileSystemEventArgs[]> events = null;
153
            var batch = new FileEventIdleBatch(100, dict => events = dict);
154
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Created, "path1", "file1"));
155
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
156
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Deleted, "path1", "file1"));
157
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Created, "path1", "file1"));
158
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
159
            Thread.Sleep(500);
160

    
161
            Assert.That(events[@"path1\file1"].Select(arg => arg.ChangeType), Is.EquivalentTo(new[] { WatcherChangeTypes.Created }));
162
        }
163

    
164
        public void TestCreateCreateSequence()
165
        {
166
            Dictionary<string, FileSystemEventArgs[]> events = null;
167
            var batch = new FileEventIdleBatch(100, dict => events = dict);
168
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Created, "path1", "file1"));
169
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
170
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Created, "path1", "file2"));
171
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file2"));
172
            Thread.Sleep(500);
173

    
174
            Assert.That(events[@"path1\file1"].Select(arg => arg.ChangeType), Is.EquivalentTo(new[] { WatcherChangeTypes.Created }));
175
            Assert.That(events[@"path1\file2"].Select(arg => arg.ChangeType), Is.EquivalentTo(new[] { WatcherChangeTypes.Created }));
176
        }
177

    
178
        [Test]
179
        public void TestCreateRenameSequence()
180
        {
181
            Dictionary<string, FileSystemEventArgs[]> events = null;
182
            var batch = new FileEventIdleBatch(100, dict => events = dict);
183
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Created, "path1", "file1"));
184
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
185
            batch.Post(new RenamedEventArgs(WatcherChangeTypes.Renamed, "path1", "file2", "file1"));
186
            Thread.Sleep(500);
187

    
188
            Assert.That(events.ContainsKey("path1\\file1"), Is.False);
189
            var changes = events[@"path1\file2"];
190
            Assert.That(changes.Length,Is.EqualTo(1));
191
            var change = changes[0];
192
            Assert.That(change.ChangeType, Is.EqualTo(WatcherChangeTypes.Created));
193
            Assert.That(change.FullPath,Is.EqualTo("path1\\file2"));                        
194
        }
195

    
196
        [Test]
197
        public void TestRenameChangeSequence()
198
        {
199
            Dictionary<string, FileSystemEventArgs[]> events = null;
200
            var batch = new FileEventIdleBatch(100, dict => events = dict);
201
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
202
            batch.Post(new RenamedEventArgs(WatcherChangeTypes.Renamed, "path1", "file2","file1"));
203
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file2"));
204
            Thread.Sleep(500);
205

    
206
            Assert.That(events.ContainsKey("path1\\file1"),Is.False);
207
            Assert.That(events["path1\\file2"].Select(arg => arg.ChangeType), Is.EquivalentTo(new[] { WatcherChangeTypes.Renamed, WatcherChangeTypes.Changed }));
208
        }
209

    
210
        [Test]
211
        public void TestCreateRenameChangeSequence()
212
        {
213
            Dictionary<string, FileSystemEventArgs[]> events = null;
214
            var batch = new FileEventIdleBatch(100, dict => events = dict);
215
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Created, "path1", "file1"));
216
            batch.Post(new RenamedEventArgs(WatcherChangeTypes.Renamed, "path1", "file2","file1"));
217
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file2"));
218
            Thread.Sleep(500);
219

    
220
            Assert.That(events.ContainsKey("path1\\file1"), Is.False);
221
            var changes = events["path1\\file2"];
222
            Assert.That(changes.Length, Is.EqualTo(1));
223
            var change = changes[0];
224
            Assert.That(change.ChangeType, Is.EqualTo(WatcherChangeTypes.Created));
225
            Assert.That(change.FullPath, Is.EqualTo(@"path1\file2"));
226
            
227
        }
228

    
229
        [Test]
230
        public void TestCreateRenameChangeDeleteSequence()
231
        {
232
            Dictionary<string, FileSystemEventArgs[]> events = null;
233
            var batch = new FileEventIdleBatch(100, dict => events = dict);
234
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Created, "path1", "file1"));
235
            batch.Post(new RenamedEventArgs(WatcherChangeTypes.Renamed, "path1", "file2","file1"));
236
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file2"));
237
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Deleted, "path1", "file2"));
238
            Thread.Sleep(500);
239

    
240
            Assert.That(events.ContainsKey("path1\\file1"), Is.False);
241
            Assert.That(events.ContainsKey("path1\\file2"), Is.False);
242
            
243
        }
244

    
245
        [Test]
246
        public void TestCreateRenameOtherSequence()
247
        {
248
            Dictionary<string, FileSystemEventArgs[]> events = null;
249
            var batch = new FileEventIdleBatch(100,dict=>events=dict);
250
            
251
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Created, "path1", "file1"));
252
            batch.Post(new FileSystemEventArgs(WatcherChangeTypes.Changed, "path1", "file1"));
253
            batch.Post(new RenamedEventArgs(WatcherChangeTypes.Renamed, "path1", "file3","file2"));
254
            Thread.Sleep(500);
255
            
256

    
257
            Assert.That(events.ContainsKey("path1\\file1"),Is.True);
258
            Assert.That(events.ContainsKey("path1\\file2"), Is.False);
259
            Assert.That(events[@"path1\file3"].Select(arg => arg.ChangeType), Is.EquivalentTo(new[] { WatcherChangeTypes.Renamed }));
260
        }
261
    }
262
}