Skip to content

Commit b7dab85

Browse files
committed
improve test case
1 parent 583e66d commit b7dab85

File tree

6 files changed

+36
-11
lines changed

6 files changed

+36
-11
lines changed

python/pyspark/streaming/dstream.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from pyspark.rdd import _JavaStackTrace
2626
from pyspark.storagelevel import StorageLevel
2727
from pyspark.resultiterable import ResultIterable
28-
from pyspark.streaming.utils import rddToFileName, RDDFunction
28+
from pyspark.streaming.util import rddToFileName, RDDFunction
2929

3030

3131
from py4j.java_collections import ListConverter, MapConverter

python/pyspark/streaming/duration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616
#
1717

18-
from pyspark.streaming import utils
18+
from pyspark.streaming import util
1919

2020

2121
class Duration(object):
@@ -82,7 +82,7 @@ def prettyPrint(self):
8282
>>> d_1hour.prettyPrint()
8383
'1.00 h'
8484
"""
85-
return utils.msDurationToString(self._millis)
85+
return util.msDurationToString(self._millis)
8686

8787
def milliseconds(self):
8888
"""

python/pyspark/streaming/jtime.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# limitations under the License.
1616
#
1717

18-
from pyspark.streaming import utils
1918
from pyspark.streaming.duration import Duration
2019

2120
"""
@@ -87,7 +86,7 @@ def __sub__(self, other):
8786
if isinstance(other, Duration):
8887
return Time(self._millis - other._millis)
8988
elif isinstance(other, Time):
90-
return Duration(self._mills, other._millis)
89+
return Duration(self._millis, other._millis)
9190
else:
9291
raise TypeError
9392

@@ -99,7 +98,7 @@ def __lt__(self, other):
9998
def __le__(self, other):
10099
""" Time <= Time """
101100
Time._is_time(other)
102-
return self.millis <= other._millis
101+
return self._millis <= other._millis
103102

104103
def __eq__(self, other):
105104
""" Time == Time """
@@ -121,7 +120,7 @@ def __ge__(self, other):
121120
Time._is_time(other)
122121
return self._millis >= other._millis
123122

124-
def isMultipbleOf(duration):
123+
def isMultipbleOf(self, duration):
125124
""" is multiple by Duration """
126125
Duration._is_duration(duration)
127126
return self._millis % duration._millis == 0

python/pyspark/streaming/tests.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@
2828
"""
2929
from itertools import chain
3030
import time
31-
import unittest
3231
import operator
32+
import sys
33+
34+
if sys.version_info[:2] <= (2, 6):
35+
import unittest2 as unittest
36+
else:
37+
import unittest
3338

3439
from pyspark.context import SparkContext
3540
from pyspark.streaming.context import StreamingContext
@@ -451,3 +456,4 @@ def _run_stream(self, test_input, test_func, expected_output, numSlices=None):
451456

452457
if __name__ == "__main__":
453458
unittest.main()
459+
SparkContext._gateway._shutdown_callback_server()

python/pyspark/streaming/utils.py renamed to python/pyspark/streaming/util.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
class RDDFunction():
2222
"""
23-
This class is for py4j callback. This
23+
This class is for py4j callback. This class is related with
24+
org.apache.spark.streaming.api.python.PythonRDDFunction.
2425
"""
2526
def __init__(self, ctx, jrdd_deserializer, func):
2627
self.ctx = ctx
@@ -41,10 +42,19 @@ class Java:
4142

4243

4344
def msDurationToString(ms):
44-
#TODO: add doctest
4545
"""
4646
Returns a human-readable string representing a duration such as "35ms"
47+
48+
>> msDurationToString(10)
49+
'10 ms'
50+
>>> msDurationToString(1000)
51+
'1.0 s'
52+
>>> msDurationToString(60000)
53+
'1.0 m'
54+
>>> msDurationToString(3600000)
55+
'1.00 h'
4756
"""
57+
#TODO: add doctest
4858
second = 1000
4959
minute = 60 * second
5060
hour = 60 * minute
@@ -60,7 +70,15 @@ def msDurationToString(ms):
6070

6171

6272
def rddToFileName(prefix, suffix, time):
63-
#TODO: add doctest
73+
"""
74+
Return string prefix-time(.suffix)
75+
76+
>>> rddToFileName("spark", None, 12345678910)
77+
'spark-12345678910'
78+
>>> rddToFileName("spark", "tmp", 12345678910)
79+
'spark-12345678910.tmp'
80+
81+
"""
6482
if suffix is None:
6583
return prefix + "-" + str(time)
6684
else:

python/run-tests

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ run_test "pyspark/broadcast.py"
6969
run_test "pyspark/accumulators.py"
7070
run_test "pyspark/serializers.py"
7171
run_test "pyspark/streaming/duration.py"
72+
run_test "pyspark/streaming/util.py"
7273
unset PYSPARK_DOC_TEST
7374
run_test "pyspark/shuffle.py"
7475
run_test "pyspark/tests.py"
@@ -81,6 +82,7 @@ run_test "pyspark/mllib/recommendation.py"
8182
run_test "pyspark/mllib/regression.py"
8283
run_test "pyspark/mllib/tests.py"
8384
run_test "pyspark/mllib/util.py"
85+
run_test "pyspark/streaming/tests.py"
8486

8587
if [[ $FAILED == 0 ]]; then
8688
echo -en "\033[32m" # Green

0 commit comments

Comments
 (0)