Skip to content

Commit c1a9233

Browse files
committed
refactor for testing
1 parent c818556 commit c1a9233

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

R/pkg/R/client.R

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,36 @@ connectBackend <- function(hostname, port, timeout = 6000) {
3434
con
3535
}
3636

37-
launchBackend <- function(args, sparkHome, jars, sparkSubmitOpts, packages) {
37+
determineSparkSubmitBin <- function() {
3838
if (.Platform$OS.type == "unix") {
3939
sparkSubmitBinName = "spark-submit"
4040
} else {
4141
sparkSubmitBinName = "spark-submit.cmd"
4242
}
43+
sparkSubmitBinName
44+
}
4345

44-
if (sparkHome != "") {
45-
sparkSubmitBin <- file.path(sparkHome, "bin", sparkSubmitBinName)
46-
} else {
47-
sparkSubmitBin <- sparkSubmitBinName
48-
}
49-
46+
generateSparkSubmitArgs <- function(args, sparkHome, jars, sparkSubmitOpts, packages) {
5047
if (jars != "") {
5148
jars <- paste("--jars", jars)
5249
}
5350

5451
if (packages != "") {
55-
packages <- paste("--packages", packages)
52+
packages <- paste("--packages", packages)
5653
}
5754

5855
combinedArgs <- paste(jars, packages, sparkSubmitOpts, args, sep = " ")
56+
combinedArgs
57+
}
58+
59+
launchBackend <- function(args, sparkHome, jars, sparkSubmitOpts, packages) {
60+
sparkSubmitBin <- determineSparkSubmitBin()
61+
if (sparkHome != "") {
62+
sparkSubmitBin <- file.path(sparkHome, "bin", sparkSubmitBinName)
63+
} else {
64+
sparkSubmitBin <- sparkSubmitBinName
65+
}
66+
combinedArgs <- generateSparkSubmitArgs(args, sparkHome, jars, sparkSubmitOpts, packages)
5967
cat("Launching java with spark-submit command", sparkSubmitBin, combinedArgs, "\n")
6068
invisible(system2(sparkSubmitBin, combinedArgs, wait = F))
6169
}

R/pkg/R/sparkR.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ sparkR.stop <- function() {
8181
#' @param sparkExecutorEnv Named list of environment variables to be used when launching executors.
8282
#' @param sparkJars Character string vector of jar files to pass to the worker nodes.
8383
#' @param sparkRLibDir The path where R is installed on the worker nodes.
84-
#' @param sparkPackages Character string vector of packages
84+
#' @param sparkPackages Character string vector of packages from spark-packages.org
8585
#' @export
8686
#' @examples
8787
#'\dontrun{
@@ -102,7 +102,7 @@ sparkR.init <- function(
102102
sparkExecutorEnv = list(),
103103
sparkJars = "",
104104
sparkRLibDir = "",
105-
sparkPackages="") {
105+
sparkPackages = "") {
106106

107107
if (exists(".sparkRjsc", envir = .sparkREnv)) {
108108
cat("Re-using existing Spark Context. Please stop SparkR with sparkR.stop() or restart R to create a new Spark Context\n")

R/pkg/inst/tests/test_client.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
context("functions in client.R")
19+
20+
test_that("adding spark-testing-base as a package works", {
21+
args <- generateSparkSubmitArgs("", "", "", "",
22+
"holdenk:spark-testing-base:1.3.0_0.0.5")
23+
expect_equal(args,
24+
"--packages holdenk:spark-testing-base:1.3.0_0.0.5")
25+
})
26+
27+
test_that("no package specified doesn't add packages flag", {
28+
args <- generateSparkSubmitArgs("", "", "", "", "")
29+
expect_equal(args, "")
30+
})

0 commit comments

Comments
 (0)