@@ -25,11 +25,24 @@ import org.json4s._
2525import org .json4s .jackson .JsonMethods
2626import org .scalatest .FunSuite
2727
28- import org .apache .spark .{ JsonTestUtils , SparkConf }
29- import org .apache .spark .deploy .DeployMessages . WorkerStateResponse
28+ import org .apache .spark .deploy . DeployMessages .{ MasterStateResponse , WorkerStateResponse }
29+ import org .apache .spark .deploy .master .{ ApplicationInfo , DriverInfo , RecoveryState , WorkerInfo }
3030import org .apache .spark .deploy .worker .{DriverRunner , ExecutorRunner }
31+ import org .apache .spark .SparkConf
3132
32- class JsonProtocolSuite extends FunSuite with JsonTestUtils {
33+ class JsonProtocolSuite extends FunSuite {
34+
35+ test(" writeApplicationInfo" ) {
36+ val output = JsonProtocol .writeApplicationInfo(createAppInfo())
37+ assertValidJson(output)
38+ assertValidDataInJson(output, JsonMethods .parse(JsonConstants .appInfoJsonStr))
39+ }
40+
41+ test(" writeWorkerInfo" ) {
42+ val output = JsonProtocol .writeWorkerInfo(createWorkerInfo())
43+ assertValidJson(output)
44+ assertValidDataInJson(output, JsonMethods .parse(JsonConstants .workerInfoJsonStr))
45+ }
3346
3447 test(" writeApplicationDescription" ) {
3548 val output = JsonProtocol .writeApplicationDescription(createAppDesc())
@@ -43,6 +56,26 @@ class JsonProtocolSuite extends FunSuite with JsonTestUtils {
4356 assertValidDataInJson(output, JsonMethods .parse(JsonConstants .executorRunnerJsonStr))
4457 }
4558
59+ test(" writeDriverInfo" ) {
60+ val output = JsonProtocol .writeDriverInfo(createDriverInfo())
61+ assertValidJson(output)
62+ assertValidDataInJson(output, JsonMethods .parse(JsonConstants .driverInfoJsonStr))
63+ }
64+
65+ test(" writeMasterState" ) {
66+ val workers = Array (createWorkerInfo(), createWorkerInfo())
67+ val activeApps = Array (createAppInfo())
68+ val completedApps = Array [ApplicationInfo ]()
69+ val activeDrivers = Array (createDriverInfo())
70+ val completedDrivers = Array (createDriverInfo())
71+ val stateResponse = new MasterStateResponse (
72+ " host" , 8080 , None , workers, activeApps, completedApps,
73+ activeDrivers, completedDrivers, RecoveryState .ALIVE )
74+ val output = JsonProtocol .writeMasterState(stateResponse)
75+ assertValidJson(output)
76+ assertValidDataInJson(output, JsonMethods .parse(JsonConstants .masterStateJsonStr))
77+ }
78+
4679 test(" writeWorkerState" ) {
4780 val executors = List [ExecutorRunner ]()
4881 val finishedExecutors = List [ExecutorRunner ](createExecutorRunner(), createExecutorRunner())
@@ -60,6 +93,13 @@ class JsonProtocolSuite extends FunSuite with JsonTestUtils {
6093 new ApplicationDescription (" name" , Some (4 ), 1234 , cmd, " appUiUrl" )
6194 }
6295
96+ def createAppInfo () : ApplicationInfo = {
97+ val appInfo = new ApplicationInfo (JsonConstants .appInfoStartTime,
98+ " id" , createAppDesc(), JsonConstants .submitDate, null , Int .MaxValue )
99+ appInfo.endTime = JsonConstants .currTimeInMillis
100+ appInfo
101+ }
102+
63103 def createDriverCommand () = new Command (
64104 " org.apache.spark.FakeClass" , Seq (" some arg --and-some options -g foo" ),
65105 Map ((" K1" , " V1" ), (" K2" , " V2" )), Seq (" cp1" , " cp2" ), Seq (" lp1" , " lp2" ), Seq (" -Dfoo" )
@@ -68,6 +108,15 @@ class JsonProtocolSuite extends FunSuite with JsonTestUtils {
68108 def createDriverDesc () = new DriverDescription (" hdfs://some-dir/some.jar" , 100 , 3 ,
69109 false , createDriverCommand())
70110
111+ def createDriverInfo (): DriverInfo = new DriverInfo (3 , " driver-3" ,
112+ createDriverDesc(), new Date ())
113+
114+ def createWorkerInfo (): WorkerInfo = {
115+ val workerInfo = new WorkerInfo (" id" , " host" , 8080 , 4 , 1234 , null , 80 , " publicAddress" )
116+ workerInfo.lastHeartbeat = JsonConstants .currTimeInMillis
117+ workerInfo
118+ }
119+
71120 def createExecutorRunner (): ExecutorRunner = {
72121 new ExecutorRunner (" appId" , 123 , createAppDesc(), 4 , 1234 , null , " workerId" , " host" , 123 ,
73122 " publicAddress" , new File (" sparkHome" ), new File (" workDir" ), " akka://worker" ,
@@ -87,6 +136,15 @@ class JsonProtocolSuite extends FunSuite with JsonTestUtils {
87136 }
88137 }
89138
139+ def assertValidDataInJson (validateJson : JValue , expectedJson : JValue ) {
140+ val Diff (c, a, d) = validateJson diff expectedJson
141+ val validatePretty = JsonMethods .pretty(validateJson)
142+ val expectedPretty = JsonMethods .pretty(expectedJson)
143+ val errorMessage = s " Expected: \n $expectedPretty\n Found: \n $validatePretty"
144+ assert(c === JNothing , s " $errorMessage\n Changed: \n ${JsonMethods .pretty(c)}" )
145+ assert(a === JNothing , s " $errorMessage\n Added: \n ${JsonMethods .pretty(a)}" )
146+ assert(d === JNothing , s " $errorMessage\n Delected: \n ${JsonMethods .pretty(d)}" )
147+ }
90148}
91149
92150object JsonConstants {
0 commit comments