示例#1
0
        public void SyncLocalInsert()
        {
            var serverUri = "http://localhost:8081";

            SimpleServerMethods.ResetData();

            using (var server = new SimpleServer(serverUri,
                                                 getMethod: SimpleServerMethods.GetMultiplePeople,
                                                 postMethod: SimpleServerMethods.PostPeople))
            {
                server.Start();

                DfdsJsonSerializer serializer = new DfdsJsonSerializer();

                var settings = new DfdsServiceSettings();
                settings.DefaultSyncPeriodSeconds = 5;

                settings.LocalStore  = new MemoryStore();
                settings.RemoteStore = new RestBasedStore(serverUri, serializer);

                bool syncComplete = false;

                var svc = new DeviceFirstDataService(settings);
                svc.SyncCompleted += delegate { syncComplete = true; };

                svc.Register <Person>("PersonID");
                // local store will be empty
                Assert.AreEqual(0, settings.LocalStore.Count <Person>());

                // when sync happens, we'll have 3
                while (!syncComplete)
                {
                    Thread.Sleep(500);
                }
                syncComplete = false;

                svc.Store(new Person("Lola L-O-L-A Lola"));

                // initial population of the store is 3
                Assert.AreEqual(4, settings.LocalStore.Count <Person>());

                // the query above triggers an insert on the remote, local still will be 3
                while (!syncComplete)
                {
                    Thread.Sleep(500);
                }
                syncComplete = false;

                Assert.AreEqual(4, settings.LocalStore.Count <Person>());
            }
        }
示例#2
0
        public void SyncLocalUpdate()
        {
            var serverUri = "http://localhost:8081";

            SimpleServerMethods.ResetData();

            using (var server = new SimpleServer(serverUri,
                                                 getMethod: SimpleServerMethods.GetMultiplePeople,
                                                 postMethod: SimpleServerMethods.PostPeople,
                                                 putMethod: SimpleServerMethods.PutPerson))
            {
                server.Start();

                DfdsJsonSerializer serializer = new DfdsJsonSerializer();

                var settings = new DfdsServiceSettings();
                settings.DefaultSyncPeriodSeconds = 5;

                settings.LocalStore  = new MemoryStore();
                settings.RemoteStore = new RestBasedStore(serverUri, serializer);

                bool syncComplete = false;

                var svc = new DeviceFirstDataService(settings);
                svc.SyncCompleted += delegate { syncComplete = true; };

                svc.Register <Person>("PersonID");
                // local store will be empty
                Assert.AreEqual(0, settings.LocalStore.Count <Person>());

                // when sync happens, we'll have 3
                while (!syncComplete)
                {
                    Thread.Sleep(500);
                }
                syncComplete = false;

                var person = svc.GetSingle <Person>(1);

                // make a change
                person.Name = "Mr. Bojangles";
                svc.Store(person);

                // wait for the change to sync
                while (!syncComplete)
                {
                    Thread.Sleep(500);
                }
                syncComplete = false;

                // does the remote match?
                var check = SimpleServerMethods.People.First(p => p.PersonID == 1);

                Assert.AreEqual(person.Name, check.Name);

                while (!syncComplete)
                {
                    Thread.Sleep(500);
                }

                Assert.AreEqual(3, settings.LocalStore.Count <Person>());
            }
        }
示例#3
0
        public void BasicSyncOperationsTest()
        {
            var settings = new DfdsServiceSettings()
            {
                LocalStore  = new MemoryStore(),
                RemoteStore = new NopRemoteStore()
            };

            var svc = new DeviceFirstDataService(settings);

            svc.Register <Person>("PersonID");

            var person1 = new Person("John Doe");
            var person2 = new Person("Jane Doe");
            var person3 = new Person("Marie Smith");

            // Store
            var p1 = svc.Store(person1);

            Assert.IsNotNull(p1);
            Assert.AreEqual(1, p1.PersonID);
            var p2 = svc.Store(person2);

            Assert.IsNotNull(p2);
            Assert.AreEqual(2, p2.PersonID);
            var p3 = svc.Store(person3);

            Assert.IsNotNull(p3);
            Assert.AreEqual(3, p3.PersonID);

            // retrieve all
            var all = svc.GetMultiple <Person>();

            Assert.AreEqual(3, all.Length);

            // retrieve by ID
            var pg1 = svc.GetSingle <Person>(1);

            Assert.AreEqual(p1, pg1);
            var pg2 = svc.GetSingle <Person>(2);

            Assert.AreEqual(p2, pg2);
            var pg3 = svc.GetSingle <Person>(3);

            Assert.AreEqual(p3, pg3);

            // update
            var newName = "Johnathan Doe";

            person1.Name = newName;
            svc.Store(person1);
            var up1 = svc.GetSingle <Person>(person1.PersonID);

            Assert.AreEqual(up1.Name, newName);

            // delete by id
            Assert.AreEqual(3, svc.Count <Person>());
            svc.Remove(person2);
            var np2 = svc.GetSingle <Person>(2);

            Assert.AreEqual(2, svc.Count <Person>());
            Assert.IsNull(np2);
            svc.Remove <Person>(3);
            var np3 = svc.GetSingle <Person>(3);

            Assert.AreEqual(1, svc.Count <Person>());
            Assert.IsNull(np3);

            // delete all
            svc.Store(person1);
            svc.Store(person2);
            svc.Store(person3);
            Assert.AreEqual(3, svc.Count <Person>());
            svc.RemoveAll <Person>();
            Assert.AreEqual(0, svc.Count <Person>());
        }