public async Task SynchronizeInsertShouldRemoveIdAndTimestamp()
        {
            ISynchronizer synchronizer = new TimestampSynchronizer(this.storage.Object);

            #region Setup
            HttpRequestMessage req = null;
            string sendjson = null;

            this.storage.Setup(iss => iss.GetStoredData(It.IsAny<string>(), It.Is<IQueryOptions>(iqo => iqo.Filter.RawValue.Equals("status ne 0")))).Returns(() =>
            {
                return Task.FromResult(new JArray(JObject.Parse(
                        @"{
                            ""id"": ""B1A60844-236A-43EA-851F-7DCD7D5755FA"",
                            ""status"": 1,
                            ""__version"": ""00000000"",
                            ""text"": ""text""
                        }"
                    )));
            });
            this.http.Setup(h => h.SendAsync(It.IsAny<HttpRequestMessage>()))
                .Returns<HttpRequestMessage>(async r =>
                {
                    req = r;
                    sendjson = await r.Content.ReadAsStringAsync();
                    return new HttpResponseMessage() { Content = responseContent };
                });
            #endregion

            await synchronizer.UploadChanges(new Uri("http://localhost/tables/table/"), this.http.Object);

            Assert.IsNotNull(req);
            IDictionary<string, JToken> obj = JObject.Parse(sendjson);

            Assert.IsTrue(obj.ContainsKey("id"));
            Assert.IsFalse(obj.ContainsKey("status"));
            Assert.IsFalse(obj.ContainsKey("__version"));
            Assert.AreEqual(HttpMethod.Post, req.Method);
        }
예제 #2
0
        public void SetSynchronizer(TimestampSynchronizer sync)
        {
            TimestampSynchronizer old = mSync;

            if (old != null)
            {
                old.Deactivate();
            }

            mSync = sync;

            /* Ok, now; synchronizer can tell us what is the first timestamp
             * value that definitely was NOT used by the previous incarnation.
             * This can serve as the last used time stamp, assuming it is not
             * less than value we are using now.
             */
            if (sync != null)
            {
                long lastSaved = sync.Initialize();
                if (lastSaved > mLastUsedTimestamp)
                {
                    mLastUsedTimestamp = lastSaved;
                }
            }

            /* Also, we need to make sure there are now no safe values (since
             * synchronizer is not yet requested to allocate any):
             */
            mFirstUnsafeTimestamp = 0L; // ie. will always trigger sync.update()
        }
예제 #3
0
 /**
  * Method that can (and should) be called once right after getting
  * the instance, to ensure that system time stamp values used are
  * valid (with respect to values used earlier by JUG instances), and
  * to use file-lock based synchronization mechanism to prevent multiple
  * JVMs from running conflicting instances of JUG (first one to be
  * started wins on contention). It can also be called to stop
  * synchronization by calling it with argument null, although such
  * usage is strongly discouraged (ie. it's a good idea to either never
  * use synchronization, or always; but not to mix modes).
  *<p>
  * Caller needs to instantiate an instance of
  * {@link TimestampSynchronizer}; currently the only standard
  * implementation is
  * {@link org.safehaus.uuid.ext.FileBasedTimestampSynchronizer} (which
  * is JDK 1.4+).
  *<p>
  * Note: since the generator instance is a singleton, calling this
  * method will always cause all generation to be synchronized using
  * the specified method.
  *
  * @param sync Synchronizer instance to use for synchronization.
  */
 public void SynchronizeExternally(TimestampSynchronizer sync)
 {
     lock (mTimerLock)
     {
         if (mTimer == null)
         {
             mTimer = new UUIDTimer(GetRandomNumberGenerator());
         }
         mTimer.SetSynchronizer(sync);
     }
 }
        public async Task DownloadChangesShouldLoadTimestampsWhenOnline()
        {
            ISynchronizer synchronizer = new TimestampSynchronizer(this.storage.Object);
            string url = "http://localhost/tables/table/";
            string timestamp = "00000000";

            #region Setup

            HttpResponseMessage response = new HttpResponseMessage();
            response.Content = new StringContent(
                @"{
                    ""results"": [],
                    ""deleted"": [],
                    ""__version"": ""00000001""
                }");

            this.storage.Setup(iss => iss.GetStoredData(It.Is<string>(str => str.Equals("timestamp_requests")), It.IsAny<IQueryOptions>()))
                .Returns(() =>
                {
                    return Task.FromResult(new JArray(JObject.Parse(string.Format(
                        //escape here
                            @"{{
                                ""requesturl"": ""{0}"",
                                ""id"": ""B1A60844-236A-43EA-851F-7DCD7D5755FA"",
                                ""__version"": ""{1}""
                            }}"
                        , url, timestamp))));
                });
            this.storage.Setup(iss => iss.StoreData(It.IsAny<string>(), It.IsAny<JArray>(), It.IsAny<bool>()))
                .Returns(() =>
                {
                    return Task.FromResult(0);
                });
            this.storage.Setup(iss => iss.RemoveStoredData(It.IsAny<string>(), It.IsAny<IEnumerable<string>>()))
                .Returns(() =>
                {
                    return Task.FromResult(0);
                });
            HttpRequestMessage request = new HttpRequestMessage();
            request.Method = HttpMethod.Get;
            this.http.Setup(h => h.SendOriginalAsync()).Returns(() => Task.FromResult(response));
            this.http.SetupGet(h => h.OriginalRequest).Returns(request);

            #endregion

            await synchronizer.DownloadChanges(new Uri(url), this.http.Object);

            Assert.AreEqual(HttpMethod.Get, request.Method);
            Assert.AreEqual(string.Format("{0}&version={1}", url, Uri.EscapeDataString(timestamp)), request.RequestUri.OriginalString);
        }
        public async Task SynchronizeDeleteShouldAddIdAndVersionToUrlAndSendEmptyBody()
        {
            ISynchronizer synchronizer = new TimestampSynchronizer(this.storage.Object);

            #region Setup
            HttpRequestMessage req = null;

            this.storage.Setup(iss => iss.GetStoredData(It.IsAny<string>(), It.Is<IQueryOptions>(iqo => iqo.Filter.RawValue.Equals("status ne 0")))).Returns(() =>
            {
                return Task.FromResult(new JArray(JObject.Parse(
                        @"{
                            ""id"": ""B1A60844-236A-43EA-851F-7DCD7D5755FA"",
                            ""status"": 3,
                            ""__version"": ""00000000"",
                            ""text"": ""text""
                        }"
                    )));
            });
            this.http.Setup(h => h.SendAsync(It.IsAny<HttpRequestMessage>()))
                .Callback<HttpRequestMessage>(r => req = r)
                .Returns(() => Task.FromResult(new HttpResponseMessage() { Content = responseContent }));

            #endregion

            await synchronizer.UploadChanges(new Uri("http://localhost/tables/table/"), this.http.Object);

            Assert.IsNotNull(req);
            HttpContent sendContent = req.Content;

            Assert.IsNull(sendContent);
            Assert.AreEqual(HttpMethod.Delete, req.Method);
            Assert.IsTrue(req.RequestUri.OriginalString.EndsWith("B1A60844-236A-43EA-851F-7DCD7D5755FA?version=00000000"));
        }