Home | History | Annotate | Download | only in gae-jdk8
      1 // Copyright 2017 The gRPC Authors
      2 //
      3 //  Licensed under the Apache License, Version 2.0 (the "License");
      4 //  you may not use this file except in compliance with the License.
      5 //  You may obtain a copy of the License at
      6 //
      7 //        http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 //  Unless required by applicable law or agreed to in writing, software
     10 //  distributed under the License is distributed on an "AS IS" BASIS,
     11 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 //  See the License for the specific language governing permissions and
     13 //  limitations under the License.
     14 description = 'gRPC: gae interop testing (jdk8)'
     15 
     16 buildscript {
     17     // Configuration for building
     18     repositories {
     19         jcenter()    // Bintray's repository - a fast Maven Central mirror & more
     20         maven { // The google mirror is less flaky than mavenCentral()
     21             url "https://maven-central.storage-download.googleapis.com/repos/central/data/" }
     22     }
     23     dependencies {
     24         classpath 'com.google.cloud.tools:appengine-gradle-plugin:1.3.5'
     25         classpath 'com.squareup.okhttp:okhttp:2.5.0'
     26     }
     27 }
     28 
     29 repositories {
     30     // repositories for Jar's you access in your code
     31     mavenLocal()
     32     maven { // The google mirror is less flaky than mavenCentral()
     33         url "https://maven-central.storage-download.googleapis.com/repos/central/data/" }
     34     jcenter()
     35 }
     36 
     37 apply plugin: 'java'                              // standard Java tasks
     38 apply plugin: 'war'                               // standard Web Archive plugin
     39 apply plugin: 'com.google.cloud.tools.appengine'  // App Engine tasks
     40 
     41 dependencies {
     42     providedCompile group: 'javax.servlet', name: 'servlet-api', version:'2.5'
     43     compile 'com.google.appengine:appengine-api-1.0-sdk:1.9.59'
     44     // Deps needed by all gRPC apps in GAE
     45     compile libraries.google_api_protos
     46     compile project(":grpc-okhttp")
     47     compile project(":grpc-protobuf")
     48     compile project(":grpc-stub")
     49     compile project(":grpc-interop-testing")
     50     compile libraries.netty_tcnative
     51 }
     52 
     53 compileJava {
     54     // Disable "No processor claimed any of these annotations: org.junit.Ignore"
     55     options.compilerArgs += ["-Xlint:-processing"]
     56 }
     57 
     58 def createDefaultVersion() {
     59     return new java.text.SimpleDateFormat("yyyyMMdd't'HHmmss").format(new Date())
     60 }
     61 
     62 // [START model]
     63 appengine {
     64     // App Engine tasks configuration
     65     run {      // local (dev_appserver) configuration (standard environments only)
     66         port = 8080                 // default
     67     }
     68 
     69     deploy {
     70         // deploy configuration
     71         // default - stop the current version
     72         stopPreviousVersion = System.getProperty('gaeStopPreviousVersion') ?: true
     73         // default - do not make this the promoted version
     74         promote = System.getProperty('gaePromote') ?: false
     75         // Use -DgaeDeployVersion if set, otherwise the version is null and the plugin will generate it
     76         version = System.getProperty('gaeDeployVersion', createDefaultVersion())
     77     }
     78 }
     79 // [END model]
     80 
     81 group = 'io.grpc'   // Generated output GroupId
     82 version = '1.0-SNAPSHOT'          // Version in generated output
     83 
     84 sourceCompatibility = 1.8
     85 targetCompatibility = 1.8
     86 
     87 /** Returns the service name. */
     88 String getGaeProject() {
     89     def stream = new ByteArrayOutputStream()
     90     exec {
     91         executable 'gcloud'
     92         args = [
     93             'config',
     94             'get-value',
     95             'project'
     96         ]
     97         standardOutput = stream
     98     }
     99     return stream.toString().trim()
    100 }
    101 
    102 String getService(java.nio.file.Path projectPath) {
    103     Node xml = new XmlParser().parse(projectPath.resolve("src/main/webapp/WEB-INF/appengine-web.xml").toFile())
    104     if (xml.service.isEmpty()) {
    105         return "default"
    106     } else {
    107         return xml.service.text()
    108     }
    109 }
    110 
    111 String getAppUrl(String project, String service, String version) {
    112     return "http://${version}.${service}.${project}.appspot.com"
    113 }
    114 
    115 task runInteropTestRemote(dependsOn: 'appengineDeploy') {
    116     doLast {
    117         // give remote app some time to settle down
    118         sleep(20000)
    119 
    120         def appUrl = getAppUrl(
    121                 getGaeProject(),
    122                 getService(project.getProjectDir().toPath()),
    123                 appengine.deploy.version)
    124         logger.log(LogLevel.INFO, "the appURL=" + appUrl)
    125         def client = new com.squareup.okhttp.OkHttpClient()
    126         // The '?jdk8' argument is ignored by the server, it exists only to tag the request log entry
    127         client.setReadTimeout(30, java.util.concurrent.TimeUnit.SECONDS)
    128         def request = new com.squareup.okhttp.Request.Builder()
    129                 .url("${appUrl}/long_lived_channel?jdk8").build()
    130         def result1 = client.newCall(request).execute()
    131         def result2 = client.newCall(request).execute()
    132         if (result1.code() != 200 || result2.code() != 200) {
    133             throw new GradleException("Unable to reuse same channel across requests")
    134         }
    135 
    136         // The test suite can take a while to run
    137         client.setReadTimeout(3, java.util.concurrent.TimeUnit.MINUTES)
    138         // The '?jdk8' argument is ignored by the server, it exists only to tag the request log entry
    139         def interopRequest = new com.squareup.okhttp.Request.Builder()
    140                 .url("${appUrl}/?jdk8").build()
    141 
    142         // Retry in case GAE is slow and times out
    143         int maxRetries = 5
    144         String result = null
    145         Throwable caught = null
    146         for (int attempt = 0; attempt < maxRetries; attempt++) {
    147             try {
    148                 def response = client.newCall(interopRequest).execute()
    149                 result = response.body().string()
    150                 project.println(result)
    151                 if (response.code() == 200) {
    152                     return
    153                 }
    154             } catch (Throwable t) {
    155                 caught = t
    156                 logger.log(LogLevel.ERROR, "caught exception. will retry if possible", t)
    157             }
    158         }
    159         throw new GradleException("Interop test failed:\nthrowable:${caught}")
    160     }
    161 }
    162