From 45647c0f48f46e0eb75eb3504d4f71ee03c4e27a Mon Sep 17 00:00:00 2001 From: yilingfeng Date: Fri, 24 Feb 2017 13:20:48 +0800 Subject: [PATCH 1/4] fix possible MQClientException when query message before today --- .../apache/rocketmq/common/message/MessageClientIDSetter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/org/apache/rocketmq/common/message/MessageClientIDSetter.java b/common/src/main/java/org/apache/rocketmq/common/message/MessageClientIDSetter.java index fe83cddeccb..a1d3ede2113 100644 --- a/common/src/main/java/org/apache/rocketmq/common/message/MessageClientIDSetter.java +++ b/common/src/main/java/org/apache/rocketmq/common/message/MessageClientIDSetter.java @@ -52,7 +52,7 @@ private synchronized static void setStartTime(long millis) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(millis); cal.set(Calendar.DAY_OF_MONTH, 1); - cal.set(Calendar.HOUR, 0); + cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); @@ -74,7 +74,7 @@ public static Date getNearlyTimeFromID(String msgID) { Calendar cal = Calendar.getInstance(); long now = cal.getTimeInMillis(); cal.set(Calendar.DAY_OF_MONTH, 1); - cal.set(Calendar.HOUR, 0); + cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); From 4b48417b4b1cfa0f2550e51f53f3c14f0619d8c1 Mon Sep 17 00:00:00 2001 From: yilingfeng Date: Wed, 8 Mar 2017 13:12:57 +0800 Subject: [PATCH 2/4] add unit tests --- .../rocketmq/common/MonStartDateTest.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 common/src/test/java/org/apache/rocketmq/common/MonStartDateTest.java diff --git a/common/src/test/java/org/apache/rocketmq/common/MonStartDateTest.java b/common/src/test/java/org/apache/rocketmq/common/MonStartDateTest.java new file mode 100644 index 00000000000..dd4294eaac4 --- /dev/null +++ b/common/src/test/java/org/apache/rocketmq/common/MonStartDateTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.rocketmq.common; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Calendar; + +public class MonStartDateTest { + + @Test + public void testMonStartTime() { + + //1488939776000 ==> 2017/3/8 10:22:56 + long AM = 1488939776000L; + long monStartTime1 = getMonStartTime(AM); + + //1488958228000 ==> 2017/3/8 15:30:28 + long PM = 1488958228000L; + long monStartTime2 = getMonStartTime(PM); + + Assert.assertEquals(1488297600000L, monStartTime1); + Assert.assertEquals(1488297600000L, monStartTime2); + Assert.assertEquals(monStartTime1, monStartTime2); + } + + public static long getMonStartTime(long time) { + + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(time); + cal.set(Calendar.DAY_OF_MONTH,1); + cal.set(Calendar.HOUR_OF_DAY,0); + cal.set(Calendar.MINUTE,0); + cal.set(Calendar.SECOND,0); + cal.set(Calendar.MILLISECOND,0); + return cal.getTimeInMillis(); + } +} \ No newline at end of file From 10171c43e36c2352e4b0f5868533da10b3db6de5 Mon Sep 17 00:00:00 2001 From: yilingfeng Date: Wed, 8 Mar 2017 14:30:18 +0800 Subject: [PATCH 3/4] modified unit tests --- .../common/MessageClientIDSetterTest.java | 41 ++++++++++++++ .../rocketmq/common/MonStartDateTest.java | 53 ------------------- 2 files changed, 41 insertions(+), 53 deletions(-) create mode 100644 common/src/test/java/org/apache/rocketmq/common/MessageClientIDSetterTest.java delete mode 100644 common/src/test/java/org/apache/rocketmq/common/MonStartDateTest.java diff --git a/common/src/test/java/org/apache/rocketmq/common/MessageClientIDSetterTest.java b/common/src/test/java/org/apache/rocketmq/common/MessageClientIDSetterTest.java new file mode 100644 index 00000000000..96fa32d0649 --- /dev/null +++ b/common/src/test/java/org/apache/rocketmq/common/MessageClientIDSetterTest.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.rocketmq.common; + +import org.apache.rocketmq.common.message.MessageClientIDSetter; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Date; + +public class MessageClientIDSetterTest { + + @Test + public void testGetNearlyTimeFromID() { + + MessageClientIDSetter messageClientIDSetter = new MessageClientIDSetter(); + //2017-03-07 09:04:00 + String message1 = "AC1F0B01327B5B2133B120D8340B3D7D"; + Date date1 = messageClientIDSetter.getNearlyTimeFromID(message1); + //2017-03-07 15:11:13 + String message2 = "AC1F0B01696C5B2133B1222866B409C9"; + Date date2 = messageClientIDSetter.getNearlyTimeFromID(message2); + + Assert.assertEquals("Tue Mar 07 09:04:00 GMT+08:00 2017", date1.toString()); + Assert.assertEquals("Tue Mar 07 15:11:13 GMT+08:00 2017", date2.toString()); + } +} \ No newline at end of file diff --git a/common/src/test/java/org/apache/rocketmq/common/MonStartDateTest.java b/common/src/test/java/org/apache/rocketmq/common/MonStartDateTest.java deleted file mode 100644 index dd4294eaac4..00000000000 --- a/common/src/test/java/org/apache/rocketmq/common/MonStartDateTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.rocketmq.common; - -import org.junit.Assert; -import org.junit.Test; - -import java.util.Calendar; - -public class MonStartDateTest { - - @Test - public void testMonStartTime() { - - //1488939776000 ==> 2017/3/8 10:22:56 - long AM = 1488939776000L; - long monStartTime1 = getMonStartTime(AM); - - //1488958228000 ==> 2017/3/8 15:30:28 - long PM = 1488958228000L; - long monStartTime2 = getMonStartTime(PM); - - Assert.assertEquals(1488297600000L, monStartTime1); - Assert.assertEquals(1488297600000L, monStartTime2); - Assert.assertEquals(monStartTime1, monStartTime2); - } - - public static long getMonStartTime(long time) { - - Calendar cal = Calendar.getInstance(); - cal.setTimeInMillis(time); - cal.set(Calendar.DAY_OF_MONTH,1); - cal.set(Calendar.HOUR_OF_DAY,0); - cal.set(Calendar.MINUTE,0); - cal.set(Calendar.SECOND,0); - cal.set(Calendar.MILLISECOND,0); - return cal.getTimeInMillis(); - } -} \ No newline at end of file From e91b85072365df42afa7854305663bfb645714d2 Mon Sep 17 00:00:00 2001 From: yilingfeng Date: Thu, 9 Mar 2017 09:49:29 +0800 Subject: [PATCH 4/4] the second modified unit tests --- .../common/MessageClientIDSetterTest.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/common/src/test/java/org/apache/rocketmq/common/MessageClientIDSetterTest.java b/common/src/test/java/org/apache/rocketmq/common/MessageClientIDSetterTest.java index 96fa32d0649..4e70e91c4bc 100644 --- a/common/src/test/java/org/apache/rocketmq/common/MessageClientIDSetterTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/MessageClientIDSetterTest.java @@ -20,22 +20,29 @@ import org.junit.Assert; import org.junit.Test; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Date; public class MessageClientIDSetterTest { @Test - public void testGetNearlyTimeFromID() { + public void testGetNearlyTimeFromID() throws ParseException { MessageClientIDSetter messageClientIDSetter = new MessageClientIDSetter(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + //2017-03-07 09:04:00 String message1 = "AC1F0B01327B5B2133B120D8340B3D7D"; - Date date1 = messageClientIDSetter.getNearlyTimeFromID(message1); + Date actualDate1 = messageClientIDSetter.getNearlyTimeFromID(message1); //2017-03-07 15:11:13 String message2 = "AC1F0B01696C5B2133B1222866B409C9"; - Date date2 = messageClientIDSetter.getNearlyTimeFromID(message2); + Date actualDate2 = messageClientIDSetter.getNearlyTimeFromID(message2); + + Date expectedDate1 = sdf.parse("2017-03-07 09:04:00"); + Date expectedDate2 = sdf.parse("2017-03-07 15:11:13"); - Assert.assertEquals("Tue Mar 07 09:04:00 GMT+08:00 2017", date1.toString()); - Assert.assertEquals("Tue Mar 07 15:11:13 GMT+08:00 2017", date2.toString()); + Assert.assertEquals(expectedDate1.toString(), actualDate1.toString()); + Assert.assertEquals(expectedDate2.toString(), actualDate2.toString()); } } \ No newline at end of file