diff --git a/build.gradle b/build.gradle index cbb72bcb..b5ab5a62 100644 --- a/build.gradle +++ b/build.gradle @@ -1,42 +1,57 @@ -buildscript { - repositories { - maven { - name = "Gradle Plugins" - url = "https://plugins.gradle.org/m2/" - } - maven { - name = "Minecraft Forge" - url = "https://files.minecraftforge.net/maven" - } - } - dependencies { - classpath "net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT" - classpath "com.github.jengelman.gradle.plugins:shadow:4.0.0" - } +plugins { + id 'eclipse' + id 'idea' + id 'maven-publish' + id 'net.minecraftforge.gradle' version '5.1.74' } -apply plugin: "net.minecraftforge.gradle.forge" -apply plugin: "com.github.johnrengelman.shadow" -apply plugin: "maven-publish" -version = "1.1.0." + ('git rev-list --count HEAD'.execute().text.trim()) + "-1.12.2" +version = "${mod_version_base}." + ('git rev-list --count HEAD'.execute().text.trim()) + "-${minecraft_version}" group = "net.buildtheearth" -archivesBaseName = "terraplusplus" - -compileJava { - sourceCompatibility = targetCompatibility = "1.8" +base { + archivesName = mod_id } +java.toolchain.languageVersion = JavaLanguageVersion.of(8) + +println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" minecraft { - version = "1.12.2-14.23.5.2847" - runDir = "run" + mappings channel: mapping_channel, version: mapping_version + + runs { + configureEach { + workingDirectory project.file('run') + + // Log4J log level for stdout + property 'forge.logging.console.level', 'debug' + + // FML debugging info dumps (comma-separated list) + // - "SCAN": For mods scan. + // - "REGISTRIES": For firing of registry events. + // - "REGISTRYDUMP": For getting the contents of all registries. + property 'forge.logging.markers', 'SCAN' + + // Handle coremods: + // - instruct FML to load the CC and CWG coremods as it doesn't know how to find them in the ForgeGradle 3+ dev environment + // - instruct Mixin to not use obfuscated names in the deobfuscated dev environment + property 'fml.coreMods.load', 'io.github.opencubicchunks.cubicchunks.core.asm.coremod.CubicChunksCoreMod,io.github.opencubicchunks.cubicchunks.cubicgen.asm.coremod.CubicGenCoreMod' + property 'mixin.env.disableRefMap', 'true' + } + + client { } - mappings = "stable_39" - makeObfSourceJar = false + server { + args 'nogui' + } + } } -configurations { - shade - compile.extendsFrom shade +// RetroGradle compatibility options for Minecraft < 1.13 support on ForgeGradle 3+. +// All enabled by default since FG detects Minecraft 1.12.2. +legacy { + // Doesn't play well with ForgeGradle 6 and the latest Forge version in runClient/runServer + // (adds the jar to the classpath instead of the compiled classes, but doesn't generate the JAR...). + // Things work well when it is disabled. + fixClasspath = true } repositories { @@ -47,67 +62,79 @@ repositories { } dependencies { - //We have to use this as deobfProvided even though there's a :dev version of the artifact because otherwise mixin - //prevents the mod from loading when running in a dev environment. - deobfProvided ("io.github.opencubicchunks:cubicchunks:1.12.2-0.0.1282.0-SNAPSHOT") { + minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" + + implementation "org.projectlombok:lombok:${lombok_version}" + annotationProcessor "org.projectlombok:lombok:${lombok_version}" + + // We have to explicitly deobfuscate this instead of using the :dev version of the artifact because otherwise mixin + // prevents the mod from loading when running in a dev environment. + implementation fg.deobf("io.github.opencubicchunks:cubicchunks:${cubicchunks_version}") { transitive = false } - - provided ("io.github.opencubicchunks:cubicworldgen:1.12.2-0.0.206.0-SNAPSHOT:dev") { + implementation("io.github.opencubicchunks:cubicworldgen:${cubicworldgen_version}:dev") { transitive = false } - shade "org.apache.commons:commons-imaging:1.0.0-alpha5" - - shade "com.fasterxml.jackson.core:jackson-databind:2.11.2" - - shade("net.daporkchop.lib:binary:0.5.7-SNAPSHOT") { + implementation "org.apache.commons:commons-imaging:${apache_commons_imaging_version}" + implementation "com.fasterxml.jackson.core:jackson-databind:${jackson_version}" + implementation("net.daporkchop.lib:binary:${porklib_version}") { exclude group: "io.netty" } - testCompile "junit:junit:4.12" - - compile "org.projectlombok:lombok:1.18.16" - annotationProcessor "org.projectlombok:lombok:1.18.16" + testImplementation(platform("org.junit:junit-bom:${junit_version}")) + testImplementation('org.junit.jupiter:junit-jupiter') + testRuntimeOnly('org.junit.platform:junit-platform-launcher') } -processResources { - // this will ensure that this task is redone when the versions change. - inputs.property "version", project.version - inputs.property "mcversion", project.minecraft.version - - // replace stuff in mcmod.info, nothing else - from(sourceSets.main.resources.srcDirs) { - include 'mcmod.info' +eclipse { + synchronizationTasks 'genEclipseRuns' +} - // replace version and mcversion - expand 'version': project.version, 'mcversion': project.minecraft.version +test { + useJUnitPlatform() + testLogging { + events "passed", "skipped", "failed" } +} - // copy everything else except the mcmod.info - from(sourceSets.main.resources.srcDirs) { - exclude 'mcmod.info' +tasks.named('processResources', ProcessResources).configure { + var replaceProperties = [ + minecraft_version: minecraft_version, + mod_id: mod_id, + mod_name: mod_name, + mod_url: mod_url, + mod_version: project.version, + ] + inputs.properties replaceProperties + + filesMatching(['mcmod.info', 'pack.mcmeta']) { + expand replaceProperties + [project: project] } } -shadowJar { - classifier = null - configurations = [project.configurations.shade] - - exclude 'module-info.class' +tasks.named('jar', Jar).configure { + manifest { + attributes([ + 'Specification-Title' : mod_id, + 'Specification-Version' : project.version, + 'Implementation-Title' : project.name, + 'Implementation-Version' : project.jar.archiveVersion, + ]) + } } -build.dependsOn shadowJar -reobf { //reobfuscate the shaded JAR - shadowJar {} +/* +sourceSets.each '{ + def dir = layout.buildDirectory.dir("sourcesSets/$it.name") + it.output.resourcesDir = dir + it.java.destinationDirectory = dir } +*/ -//relocate all shaded dependencies -task relocateShadowJar(type: com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocation) { - target = tasks.shadowJar - prefix = "net.buildtheearth.terraplusplus.dep" +tasks.withType(JavaCompile).configureEach { + options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation } -shadowJar.dependsOn relocateShadowJar publishing { publications { @@ -119,12 +146,42 @@ publishing { from components.java artifacts.clear() - artifact(shadowJar) { - classifier = "" - } - artifact sourceJar { - classifier "sources" - } } } } + +tasks.register('forgeGradleMixinCacheCompatibilityHack') { + group 'FG compatibility hacks' + description 'Renames the ForgeGradle v3+ deobfuscated dependency cache to the name used by ForgeGradle v2 and symlinks the modern name, so that MixinPlatformAgentFMLLegacy properly qualifies those dependencies.' + + doLast { + def fgCacheDir = file("${gradle.gradleUserHomeDir}/caches/forge_gradle") + def oldDir = file("${fgCacheDir}/deobf_dependencies") + def newDir = file("${fgCacheDir}/deobfedDeps") + + if (!oldDir.exists() || !oldDir.isDirectory()) { + println "ForgeGradle 3+ deobfuscated dependency cache not found at: ${oldDir}" + return + } + println "Found ForgeGradle 3+ deobfuscated dependency cache at at: ${oldDir}" + + if (newDir.exists()) { + println "ForgeGradle 2 deobfuscated dependency already exists at: ${oldDir}, nothing to do" + return + } + println "Will move cache to ForgeGradle 2 location at: ${newDir}" + + try { + oldDir.renameTo(newDir) + java.nio.file.Files.createSymbolicLink(oldDir.toPath(), newDir.toPath()) + println "Hacky workaround for MixinPlatformAgentFMLLegacy installed" + } catch (Exception e) { + throw new GradleException("Failed to create required symlink", e) + } + } +} +afterEvaluate { + tasks.named('prepareRuns') { + dependsOn forgeGradleMixinCacheCompatibilityHack + } +} diff --git a/gradle.properties b/gradle.properties index 4c72820a..efe17672 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,3 +4,29 @@ org.gradle.jvmargs=-Xmx3G # workaround: https://www.abrarsyed.com/ForgeGradleVersion.json doesn't have a valid SSL certificate any more net.minecraftforge.gradle.disableUpdateChecker=true + +# Platform versions (Minecraft, Minecraft Forge & obfuscation mappings) +minecraft_version=1.12.2 +forge_version=14.23.5.2855 +mapping_channel=stable +mapping_version=39-1.12 + +# Build time dependencies +lombok_version=1.18.16 + +# Mod dependency versions (CubicChunks & CubicWorldGen) +cubicchunks_version=1.12.2-0.0.1282.0-SNAPSHOT +cubicworldgen_version=1.12.2-0.0.206.0-SNAPSHOT + +# Library dependencies +apache_commons_imaging_version=1.0.0-alpha5 +jackson_version=2.11.2 +porklib_version=0.5.7-SNAPSHOT + +# Test dependency versions +junit_version=5.14.4 + +mod_id=terraplusplus +mod_name=TerraPlusPlus +mod_version_base=1.1.0 +mod_url=https://buildtheearth.net \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 30d399d8..afba1092 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 5f06eb19..b3715553 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Sep 14 12:28:28 PDT 2015 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.6-bin.zip +networkTimeout=10000 zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.8.1-bin.zip diff --git a/gradlew b/gradlew index f48d5d06..65dcd68d 100755 --- a/gradlew +++ b/gradlew @@ -1,170 +1,244 @@ -#!/usr/bin/env sh +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed 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 +# +# https://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. +# ############################################################################## -## -## Gradle start up script for UN*X -## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# ############################################################################## # Attempt to set APP_HOME + # Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null -APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" +MAX_FD=maximum warn () { echo "$*" -} +} >&2 die () { echo echo "$*" echo exit 1 -} +} >&2 # OS specific support (must be 'true' or 'false'). cygwin=false msys=false darwin=false nonstop=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; - NONSTOP* ) - nonstop=true - ;; +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; esac CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + # Determine the Java command to use to start the JVM. if [ -n "$JAVA_HOME" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACMD=$JAVA_HOME/jre/sh/java else - JAVACMD="$JAVA_HOME/bin/java" + JAVACMD=$JAVA_HOME/bin/java fi if [ ! -x "$JAVACMD" ] ; then die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + Please set the JAVA_HOME variable in your environment to match the location of your Java installation." fi else - JAVACMD="java" + JAVACMD=java which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + Please set the JAVA_HOME variable in your environment to match the location of your Java installation." fi # Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac fi -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - JAVACMD=`cygpath --unix "$JAVACMD"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) fi - i=$((i+1)) + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg done - case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac fi -# Escape application args -save () { - for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done - echo " " -} -APP_ARGS=$(save "$@") - -# Collect all arguments for the java command, following the shell quoting and substitution rules -eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" - -# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong -if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then - cd "$(dirname "$0")" +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" fi +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat index 8a0b282a..93e3f59f 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,4 +1,20 @@ -@if "%DEBUG%" == "" @echo off +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off @rem ########################################################################## @rem @rem Gradle startup script for Windows @@ -8,20 +24,24 @@ @rem Set local scope for the variables with windows NT shell if "%OS%"=="Windows_NT" setlocal -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= - set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init +if %ERRORLEVEL% equ 0 goto execute echo. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. @@ -35,7 +55,7 @@ goto fail set JAVA_HOME=%JAVA_HOME:"=% set JAVA_EXE=%JAVA_HOME%/bin/java.exe -if exist "%JAVA_EXE%" goto init +if exist "%JAVA_EXE%" goto execute echo. echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% @@ -45,44 +65,26 @@ echo location of your Java installation. goto fail -:init -@rem Get command-line arguments, handling Windowz variants - -if not "%OS%" == "Windows_NT" goto win9xME_args -if "%@eval[2+2]" == "4" goto 4NT_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* -goto execute - -:4NT_args -@rem Get arguments from the 4NT Shell from JP Software -set CMD_LINE_ARGS=%$ - :execute @rem Setup the command line set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + @rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* :end @rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd +if %ERRORLEVEL% equ 0 goto mainEnd :fail rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% :mainEnd if "%OS%"=="Windows_NT" endlocal diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 00000000..48527db9 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,9 @@ +pluginManagement { + repositories { + gradlePluginPortal() + maven { + name = 'MinecraftForge' + url = 'https://maven.minecraftforge.net/' + } + } +} \ No newline at end of file diff --git a/src/main/java/net/buildtheearth/terraplusplus/util/http/CacheEntry.java b/src/main/java/net/buildtheearth/terraplusplus/util/http/CacheEntry.java index 40a8dbb6..20f147e9 100644 --- a/src/main/java/net/buildtheearth/terraplusplus/util/http/CacheEntry.java +++ b/src/main/java/net/buildtheearth/terraplusplus/util/http/CacheEntry.java @@ -13,6 +13,7 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.time.Instant; +import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Map; @@ -21,6 +22,8 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; +import static java.time.ZoneOffset.*; + /** * @author DaPorkchop_ */ @@ -137,7 +140,8 @@ public void touch(@NonNull HttpHeaders headers) { if (this.etag != null) { headers.set(HttpHeaderNames.IF_NONE_MATCH, this.etag); } else if (this.staleTime >= 0L) { - headers.set(HttpHeaderNames.IF_MODIFIED_SINCE, DateTimeFormatter.RFC_1123_DATE_TIME.format(Instant.ofEpochMilli(this.time))); + ZonedDateTime lastModified = Instant.ofEpochMilli(this.time).atZone(UTC); + headers.set(HttpHeaderNames.IF_MODIFIED_SINCE, DateTimeFormatter.RFC_1123_DATE_TIME.format(lastModified)); } } diff --git a/src/main/java/net/buildtheearth/terraplusplus/util/http/HostManager.java b/src/main/java/net/buildtheearth/terraplusplus/util/http/HostManager.java index c311703d..73a4b2f6 100644 --- a/src/main/java/net/buildtheearth/terraplusplus/util/http/HostManager.java +++ b/src/main/java/net/buildtheearth/terraplusplus/util/http/HostManager.java @@ -32,6 +32,7 @@ import lombok.RequiredArgsConstructor; import lombok.ToString; import net.buildtheearth.terraplusplus.TerraConstants; +import net.buildtheearth.terraplusplus.TerraMod; import net.daporkchop.lib.common.misc.string.PStrings; import java.util.ArrayDeque; @@ -270,6 +271,8 @@ private void handleChannelClosed(@NonNull ChannelFuture channelFuture) { // but without triggering an exception. most likely the channel was a keepalive channel, // and the server closed it at the same time as we sent the request. let's re-submit the request // so that it can be issued again on a new channel + String protoPart = this.ssl ? "https://" : "http://"; + TerraMod.LOGGER.info("Channel closed early, re-submitting request: {}{}{}", protoPart, this.authority, request.path); this.pendingRequests.addFirst(request); //add to front of queue so that it doesn't have to wait through the entire queue again } @@ -334,11 +337,11 @@ public interface Callback { @ToString private final class Request { @NonNull - protected final String path; + private final String path; @NonNull - protected final Callback callback; + private final Callback callback; @NonNull - protected final HttpHeaders headers; + private final HttpHeaders headers; public HttpRequest toNetty() { DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, this.path); diff --git a/src/main/java/net/buildtheearth/terraplusplus/util/http/Http.java b/src/main/java/net/buildtheearth/terraplusplus/util/http/Http.java index 0085fad1..39439c17 100644 --- a/src/main/java/net/buildtheearth/terraplusplus/util/http/Http.java +++ b/src/main/java/net/buildtheearth/terraplusplus/util/http/Http.java @@ -4,6 +4,7 @@ import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; +import io.netty.channel.ChannelException; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.epoll.Epoll; @@ -11,10 +12,7 @@ import io.netty.channel.epoll.EpollSocketChannel; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.codec.http.DefaultHttpHeaders; -import io.netty.handler.codec.http.EmptyHttpHeaders; -import io.netty.handler.codec.http.FullHttpResponse; -import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.*; import io.netty.handler.codec.http2.Http2SecurityUtil; import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; @@ -35,9 +33,11 @@ import net.minecraft.network.NetworkSystem; import net.minecraft.util.LazyLoadBase; import net.minecraftforge.fml.common.FMLCommonHandler; +import net.minecraftforge.fml.common.IFMLSidedHandler; import net.minecraftforge.fml.relauncher.Side; import javax.net.ssl.SSLException; +import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.nio.file.Path; @@ -55,6 +55,12 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import static io.netty.handler.codec.http.HttpMethod.*; +import static java.lang.Integer.*; +import static java.lang.Math.*; +import static java.lang.System.*; +import static java.util.Objects.*; +import static java.util.concurrent.TimeUnit.*; import static net.daporkchop.lib.common.util.PValidation.*; /** @@ -64,15 +70,16 @@ */ @UtilityClass public class Http { - protected static final long TIMEOUT = 20L; + static final long TIMEOUT = 20L; - protected final EventLoopGroup NETWORK_EVENT_LOOP_GROUP; + final EventLoopGroup NETWORK_EVENT_LOOP_GROUP; static { if (!TerraConstants.IS_TEST_ENVIRONMENT && TerraConfig.http.useVanillaNetworkThread) { //use the vanilla eventloop for the current side LazyLoadBase eventLoopGroupLoader; - if (FMLCommonHandler.instance().getSide() == Side.CLIENT) { + IFMLSidedHandler fmlSideDelegate = FMLCommonHandler.instance().getSidedDelegate(); + if (fmlSideDelegate != null && fmlSideDelegate.getSide() == Side.CLIENT) { eventLoopGroupLoader = Epoll.isAvailable() ? NetworkManager.CLIENT_EPOLL_EVENTLOOP : NetworkManager.CLIENT_NIO_EVENTLOOP; } else { eventLoopGroupLoader = Epoll.isAvailable() ? NetworkSystem.SERVER_EPOLL_EVENTLOOP : NetworkSystem.SERVER_NIO_EVENTLOOP; @@ -88,7 +95,7 @@ public class Http { } } - protected final Bootstrap DEFAULT_BOOTSTRAP = new Bootstrap() + final Bootstrap DEFAULT_BOOTSTRAP = new Bootstrap() //perform name lookups asynchronously so that we can open connections without blocking the server thread. // //we aren't using the round-robin implementation (RoundRobinAsyncDefaultResolverGroup) here because it can return IPv6 addresses even if the host doesn't @@ -107,13 +114,13 @@ public class Http { .option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, toInt(TimeUnit.SECONDS.toMillis(TIMEOUT))); - protected final SslContext SSL_CONTEXT; + final SslContext SSL_CONTEXT; - protected final Map MANAGERS = new ConcurrentHashMap<>(); + private final Map MANAGERS = new ConcurrentHashMap<>(); - protected final int MAX_CONTENT_LENGTH = Integer.MAX_VALUE; //impossibly large, no requests will actually be this big but whatever + final int MAX_CONTENT_LENGTH = Integer.MAX_VALUE; //impossibly large, no requests will actually be this big but whatever - protected static final Cached URL_FORMATTING_MATCHER_CACHE = Cached.regex(Pattern.compile("\\$\\{([a-z0-9.]+)}")); + private static final Cached URL_FORMATTING_MATCHER_CACHE = Cached.regex(Pattern.compile("\\$\\{([a-z0-9.]+)}")); static { try { @@ -126,6 +133,8 @@ public class Http { } } + public static final int DEFAULT_REQUEST_TRY_COUNT = parseInt(getProperty("terraplusplus.http.defaultRequestTryCount", "5")); + public static final RequestOptions DEFAULT_REQUEST_OPTIONS = RequestOptions.builder().build(); private HostManager managerFor(@NonNull URL url) { @@ -149,6 +158,7 @@ class State implements BiConsumer, HostManager.Callback { CacheEntry cacheEntry; ByteBuf cachedData; HttpHeaders nextHeaders = EmptyHttpHeaders.INSTANCE; + int requestAttempts = 0; @Override public synchronized boolean isCancelled() { @@ -232,6 +242,23 @@ void releaseCacheEntry() { @Override public synchronized void handle(FullHttpResponse response, Throwable throwable) { //stage 2: handle HTTP response try { + this.requestAttempts++; + + if (this.shouldRetry(response, throwable)) { + long delayMillis = this.getRetryDelayMillis(); + TerraMod.LOGGER.warn( + "Will retry: {} attempt[{}/{}] delay[{}ms] reason[status: {}, throwable: {}]", + this.parsed, + this.requestAttempts + 1, options.maxTries, + delayMillis, + response == null ? null : response.status(), throwable); + NETWORK_EVENT_LOOP_GROUP.schedule( + () -> managerFor(this.parsed).submit(this.parsed.getFile(), this, this.nextHeaders), + delayMillis, MILLISECONDS + ); + return; + } + //if cacheEntry is non-null, it means we're currently attempting to refresh a stale entry if (throwable != null) { @@ -292,6 +319,26 @@ public synchronized void handle(FullHttpResponse response, Throwable throwable) } } + synchronized boolean shouldRetry(FullHttpResponse response, Throwable throwable) { + checkState(response != null || throwable != null, "Response and throwable are both null"); + if (this.requestAttempts >= options.maxTries) { + return false; // Time to give up + } + if (!isRetryableMethod(GET)) { // We only do GET requests in here + return false; + } + if (throwable != null) { + return isRetryableException(throwable); + } + return isRetryableStatus(response.status().code()); + } + + synchronized long getRetryDelayMillis() { + long baseDelay = backoffDelayMillis(this.requestAttempts + 1); + double factor = 1 + (Math.random() * options.retryDelayJitterFactor * 2) - options.retryDelayJitterFactor; + return (long) (baseDelay * factor); + } + synchronized void step(@NonNull String url) { try { this.parsed = new URL(url); @@ -464,7 +511,7 @@ public void configChanged() { for (String entry : TerraConfig.http.maxConcurrentRequests) { if (matcher.reset(entry).matches()) { try { - setMaximumConcurrentRequestsTo(matcher.group(2), Integer.parseInt(matcher.group(1))); + setMaximumConcurrentRequestsTo(matcher.group(2), parseInt(matcher.group(1))); } catch (Exception e) { TerraMod.LOGGER.error("Invalid entry: \"" + entry + '"', e); } @@ -474,7 +521,7 @@ public void configChanged() { } } - protected void copyResultTo(@NonNull CompletableFuture src, @NonNull CompletableFuture dst) { + private void copyResultTo(@NonNull CompletableFuture src, @NonNull CompletableFuture dst) { src.whenComplete((v, t) -> { if (t != null) { dst.completeExceptionally(t); @@ -508,5 +555,80 @@ public static final class RequestOptions { */ @Builder.Default public final boolean followRedirects = true; + + /** + * The maximum number of attempts for the request. + * If the first request fails and is retriable, it will be retried up to {@code maxTries - 1} times. + * The request will not be retried if the failure reason is not compatible with a retry. + * Values lower than {@code 1} will be treated as {@code 1}. + */ + @Builder.Default + public final int maxTries = DEFAULT_REQUEST_TRY_COUNT; + + /** + * The amount jitter factor to use when calculating the retry delays. + * The base retry delay will be multiplied by a random value between {@code 1 - retryDelayJitterFactor} and {@code 1 + retryDelayJitterFactor}. + */ + @Builder.Default + public final float retryDelayJitterFactor = 0.5f; + } + + private static boolean isRetryableMethod(@NonNull HttpMethod method) { + requireNonNull(method); + return method.equals(GET) || method.equals(HEAD) || method.equals(OPTIONS) || method.equals(TRACE); + } + private static boolean isRetryableException(@NonNull Throwable throwable) { + requireNonNull(throwable); + while (throwable != null) { + if (throwable instanceof IOException) { + return true; + } + if (throwable instanceof ChannelException) { + return true; // IO error in Netty + } + throwable = throwable.getCause(); + } + return false; + } + + private static boolean isRetryableStatus(int status) { + if (status < 400) { + return false; // Not an error at all + } + switch (status) { + case 408: // Request Timeout + case 429: // Too Many Requests + case 500: // Internal Server Error + case 502: // Bad Gateway + case 503: // Service Unavailable + case 504: // Gateway Timeout + return true; + default: + return false; + } } + + /** + * Exponential backoff delay in milliseconds for a given attempt. + *
+ * Timings: + *
    + *
  • 1st attempt: 0ms
  • + *
  • 2nd attempt: 100ms
  • + *
  • 3rd attempt: 400ms
  • + *
  • 4th attempt: 1.6s
  • + *
  • 5th attempt: 6.4s
  • + *
+ * + * @param attempt the attempt number + * @return the delay to wait before retrying, in milliseconds + */ + private static long backoffDelayMillis(int attempt) { + if (attempt <= 1) { + return 0L; + } + double backoffSeconds = 0.1 * Math.pow(4, attempt - 2); + return round(backoffSeconds * 1000); + } + } diff --git a/src/main/resources/mcmod.info b/src/main/resources/mcmod.info index 65ebba07..cb06e894 100644 --- a/src/main/resources/mcmod.info +++ b/src/main/resources/mcmod.info @@ -1,11 +1,11 @@ [ { - "modid": "terraplusplus", - "name": "TerraPlusPlus", + "modid": "${mod_id}", + "name": "${mod_name}", "description": "A feature-rich fork of Terra121 focusing on performance.", - "version": "${version}", - "mcversion": "${mcversion}", - "url": "https://buildtheearth.net", + "version": "${mod_version}", + "mcversion": "${minecraft_version}", + "url": "${mod_url}", "updateUrl": "", "authorList": [ "DaPorkchop_", diff --git a/src/test/java/net/buildtheearth/terraplusplus/util/OrderedRegistryTest.java b/src/test/java/net/buildtheearth/terraplusplus/util/OrderedRegistryTest.java index a6ce20b0..e8a642d9 100644 --- a/src/test/java/net/buildtheearth/terraplusplus/util/OrderedRegistryTest.java +++ b/src/test/java/net/buildtheearth/terraplusplus/util/OrderedRegistryTest.java @@ -1,12 +1,13 @@ package net.buildtheearth.terraplusplus.util; +import org.junit.jupiter.api.Test; + import static net.daporkchop.lib.common.util.PValidation.checkState; import java.util.Arrays; import java.util.Map; import java.util.stream.Collectors; -import org.junit.Test; /** * @author DaPorkchop_ diff --git a/src/test/java/net/buildtheearth/terraplusplus/util/geo/CoordinateParseUtilsTest.java b/src/test/java/net/buildtheearth/terraplusplus/util/geo/CoordinateParseUtilsTest.java index 116d348d..10146ba8 100644 --- a/src/test/java/net/buildtheearth/terraplusplus/util/geo/CoordinateParseUtilsTest.java +++ b/src/test/java/net/buildtheearth/terraplusplus/util/geo/CoordinateParseUtilsTest.java @@ -1,9 +1,9 @@ package net.buildtheearth.terraplusplus.util.geo; -import org.junit.Assert; -import org.junit.Test; -import static org.junit.Assert.*; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; /** * @author SmylerMC @@ -111,11 +111,11 @@ public void testInvalid() { private void testValidStringParsing(String string, double longitude, double latitude) { LatLng latLng = CoordinateParseUtils.parseVerbatimCoordinates(string); - Assert.assertNotNull(String.format("Failed to parse a valid coordinate string: %s", string), latLng); + assertNotNull(latLng, String.format("Failed to parse a valid coordinate string: %s", string)); final double lng = latLng.getLng(); final double lat = latLng.getLat(); - Assert.assertEquals("Parsed a wrong longitude value", longitude, lng, PRECISION); - Assert.assertEquals("Parsed a wrong latitude value", latitude, lat, PRECISION); + assertEquals(longitude, lng, PRECISION, "Parsed a wrong longitude value"); + assertEquals(latitude, lat, PRECISION, "Parsed a wrong latitude value"); } private void testInvalidStringParsing(String string) { diff --git a/src/test/java/net/buildtheearth/terraplusplus/util/http/HttpTest.java b/src/test/java/net/buildtheearth/terraplusplus/util/http/HttpTest.java new file mode 100644 index 00000000..870e370a --- /dev/null +++ b/src/test/java/net/buildtheearth/terraplusplus/util/http/HttpTest.java @@ -0,0 +1,338 @@ +package net.buildtheearth.terraplusplus.util.http; + +import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.HttpServer; +import io.netty.buffer.ByteBuf; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.io.Closeable; +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.Random; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.atomic.AtomicInteger; + +import static java.lang.System.*; +import static java.lang.Thread.*; +import static org.apache.http.HttpStatus.*; +import static org.junit.jupiter.api.Assertions.*; + +@Execution(value = ExecutionMode.CONCURRENT) +public class HttpTest { + + @Test + void canMakeRequest() throws ExecutionException, InterruptedException, IOException { + final String response = "Hello, World!"; + + HttpHandler handler = exchange -> { + try (OutputStream os = exchange.getResponseBody()) { + exchange.getResponseHeaders().set("Cache-Control", "no-cache"); + exchange.getResponseHeaders().set("Content-Type", "text/plain"); + exchange.sendResponseHeaders(SC_OK, response.getBytes().length); + os.write(response.getBytes()); + } + }; + try(TestHttpEndpoint endpoint = new TestHttpEndpoint("/hello", handler)) { + endpoint.getAndAssertStringBody("?time=" + currentTimeMillis(), response); + } + } + + @Test + void canCacheResponse() throws ExecutionException, InterruptedException, IOException { + final String response = "Cached content"; + final String suffix = "?time=" + currentTimeMillis(); + + final AtomicInteger requestCounter = new AtomicInteger(0); + + HttpHandler handler = exchange -> { + requestCounter.incrementAndGet(); + try (OutputStream os = exchange.getResponseBody()) { + exchange.getResponseHeaders().set("Cache-Control", "max-age=100"); + exchange.getResponseHeaders().set("Content-Type", "text/plain"); + exchange.sendResponseHeaders(SC_OK, response.getBytes().length); + os.write(response.getBytes()); + } + }; + try(TestHttpEndpoint endpoint = new TestHttpEndpoint("/withCache", handler)) { + endpoint.getAndAssertStringBody(suffix, response); + assertEquals(1, requestCounter.get()); + endpoint.getAndAssertStringBody(suffix, response); // Should hit the cache + assertEquals(1, requestCounter.get()); + } + } + + @Test + void canRefreshStaleResponseFromEtag() throws ExecutionException, InterruptedException, IOException { + final String response = "Cached content"; + final String etag = "foobar"; + final String suffix = "?time=" + currentTimeMillis(); + + final AtomicInteger initialRequestCounter = new AtomicInteger(0); + final AtomicInteger refreshRequestCounter = new AtomicInteger(0); + + HttpHandler handler = exchange -> { + try (OutputStream os = exchange.getResponseBody()) { + String expectedEtag = exchange.getRequestHeaders().getFirst("If-None-Match"); + if (expectedEtag != null && expectedEtag.equals(etag)) { + refreshRequestCounter.incrementAndGet(); + exchange.sendResponseHeaders(SC_NOT_MODIFIED, -1); + return; + } + initialRequestCounter.incrementAndGet(); + exchange.getResponseHeaders().set("Cache-Control", "max-age=1"); + exchange.getResponseHeaders().set("Etag", etag); + exchange.getResponseHeaders().set("Content-Type", "text/plain"); + exchange.sendResponseHeaders(SC_OK, response.getBytes().length); + os.write(response.getBytes()); + } + }; + try(TestHttpEndpoint endpoint = new TestHttpEndpoint("/withEtag", handler)) { + endpoint.getAndAssertStringBody(suffix, response); + assertEquals(1, initialRequestCounter.get()); + assertEquals(0, refreshRequestCounter.get()); + sleep(1_100); // Let the cache expire + endpoint.getAndAssertStringBody(suffix, response); // Should hit the cache but not be fresh + assertEquals(1, initialRequestCounter.get()); + assertEquals(1, refreshRequestCounter.get()); + } + } + + @Test + void canRetryOnConnectionClosed() throws ExecutionException, InterruptedException, IOException { + final String response = "Hello, World!"; + + AtomicInteger counter = new AtomicInteger(0); + + HttpHandler handler = exchange -> { + boolean shouldFail = counter.incrementAndGet() < 3; + try (OutputStream os = exchange.getResponseBody()) { + if (shouldFail) { + return; + } + exchange.getResponseHeaders().set("Cache-Control", "no-cache"); + exchange.getResponseHeaders().set("Content-Type", "text/plain"); + exchange.sendResponseHeaders(SC_OK, response.getBytes().length); + os.write(response.getBytes()); + } + }; + try(TestHttpEndpoint endpoint = new TestHttpEndpoint("/retryUntilNotClosed", handler)) { + endpoint.getAndAssertStringBody("?time=" + currentTimeMillis(), response); + } + } + + @Test + void canRetryOnConnectionTimeout() throws ExecutionException, InterruptedException, IOException { + final String response = "Hello, World!"; + + AtomicInteger counter = new AtomicInteger(0); + + HttpHandler handler = exchange -> { + boolean shouldTimeout = counter.incrementAndGet() < 2; + if (shouldTimeout) { + try { + sleep(5_000); // Will trigger io.netty.handler.timeout.ReadTimeoutException in the client + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return; + } + + try (OutputStream os = exchange.getResponseBody()) { + exchange.getResponseHeaders().set("Cache-Control", "no-cache"); + exchange.getResponseHeaders().set("Content-Type", "text/plain"); + exchange.sendResponseHeaders(SC_OK, response.getBytes().length); + os.write(response.getBytes()); + } + }; + try(TestHttpEndpoint endpoint = new TestHttpEndpoint("/retryUntilNotTimeout", handler)) { + endpoint.getAndAssertStringBody("?time=" + currentTimeMillis(), response); + } + } + + @ParameterizedTest + @ValueSource(ints = { SC_REQUEST_TIMEOUT, 429, SC_BAD_GATEWAY, SC_SERVICE_UNAVAILABLE, SC_GATEWAY_TIMEOUT}) + void canRetryOnHttpRetriableStatusCodes(int status) throws ExecutionException, InterruptedException, IOException { + final String response = "Hello, World!"; + final String plzRetryResponse = "Please retry"; + + AtomicInteger counter = new AtomicInteger(0); + + HttpHandler handler = exchange -> { + try (OutputStream os = exchange.getResponseBody()) { + boolean shouldBeUnavailable = counter.incrementAndGet() < 3; + if (shouldBeUnavailable) { + exchange.sendResponseHeaders(status, plzRetryResponse.getBytes().length); + os.write(plzRetryResponse.getBytes()); + return; + } + exchange.getResponseHeaders().set("Cache-Control", "no-cache"); + exchange.getResponseHeaders().set("Content-Type", "text/plain"); + exchange.sendResponseHeaders(SC_OK, response.getBytes().length); + os.write(response.getBytes()); + } + }; + try(TestHttpEndpoint endpoint = new TestHttpEndpoint("/retrySuccess", handler)) { + endpoint.getAndAssertStringBody("?time=" + currentTimeMillis(), response); + } + } + + @ParameterizedTest + @ValueSource(ints = {SC_BAD_REQUEST, SC_UNAUTHORIZED, SC_FORBIDDEN, SC_NOT_FOUND, SC_METHOD_NOT_ALLOWED, SC_NOT_ACCEPTABLE, SC_GONE, SC_NOT_IMPLEMENTED}) + void doesNotRetryOnNonRetriableStatusCodes(final int status) { + final String body = "plz don't retry"; + + AtomicInteger counter = new AtomicInteger(0); + + HttpHandler handler = exchange -> { + counter.incrementAndGet(); + try (OutputStream os = exchange.getResponseBody()) { + exchange.getResponseHeaders().set("Content-Type", "text/plain"); + exchange.sendResponseHeaders(status, body.getBytes().length); + os.write(body.getBytes()); + } + }; + try(TestHttpEndpoint endpoint = new TestHttpEndpoint("/notRetryable", handler)) { + Http.get(endpoint.url() + "?time=" + currentTimeMillis() + "&code=" + status).get(); + } catch (Throwable ignored) { + // It's ok if it throws an exception, as long as it did not retry + } + + assertEquals(1, counter.get()); + } + + @Test + void canRefreshStaleResponseFromLastModified() throws ExecutionException, InterruptedException, IOException { + final String response = "Cached content"; + final String suffix = "?time=" + currentTimeMillis(); + + final AtomicInteger initialRequestCounter = new AtomicInteger(0); + final AtomicInteger refreshRequestCounter = new AtomicInteger(0); + + HttpHandler handler = exchange -> { + try (OutputStream os = exchange.getResponseBody()) { + boolean ifModifiedSince = exchange.getRequestHeaders().containsKey("If-Modified-Since"); + if (ifModifiedSince) { + refreshRequestCounter.incrementAndGet(); + exchange.sendResponseHeaders(SC_NOT_MODIFIED, -1); + return; + } + initialRequestCounter.incrementAndGet(); + exchange.getResponseHeaders().set("Cache-Control", "max-age=1"); + exchange.getResponseHeaders().set("Content-Type", "text/plain"); + exchange.sendResponseHeaders(SC_OK, response.getBytes().length); + os.write(response.getBytes()); + } + }; + try(TestHttpEndpoint endpoint = new TestHttpEndpoint("/withDate", handler)) { + endpoint.getAndAssertStringBody(suffix, response); + assertEquals(1, initialRequestCounter.get()); + assertEquals(0, refreshRequestCounter.get()); + sleep(1_100); // Let the cache expire + endpoint.getAndAssertStringBody(suffix, response); // Should hit the cache but not be fresh + assertEquals(1, initialRequestCounter.get()); + assertEquals(1, refreshRequestCounter.get()); + } + } + + @ParameterizedTest + @ValueSource(ints = {1, 2, 3}) + void respectsMaxTriesInRequestOptions(int maxTries) throws Exception { + final String response = "Internal server errro"; + final AtomicInteger counter = new AtomicInteger(); + HttpHandler handler = exchange -> { + counter.incrementAndGet(); + exchange.sendResponseHeaders(SC_INTERNAL_SERVER_ERROR, response.getBytes().length); + try(OutputStream os = exchange.getResponseBody()) { + os.write(response.getBytes()); + } + }; + try(TestHttpEndpoint endpoint = new TestHttpEndpoint("/error", handler)) { + Http.RequestOptions options = Http.RequestOptions.builder().maxTries(maxTries).build(); + try { + Http.get(endpoint.url() + "?maxTries=" + maxTries, options).get(); + } catch (Exception ignored) { + // It's ok to fail as long as it tried the expected number of times + } + assertEquals(maxTries, counter.get()); + } + } + + @Test + void isDoingExponentialBackoff() throws Exception { + final String response = "Internal server error"; + final AtomicInteger counter = new AtomicInteger(); + final long[] timings = new long[5]; + HttpHandler handler = exchange -> { + int i = counter.getAndIncrement(); + timings[i] = System.currentTimeMillis(); + exchange.sendResponseHeaders(SC_INTERNAL_SERVER_ERROR, response.getBytes().length); + try (OutputStream os = exchange.getResponseBody()) { + os.write(response.getBytes()); + } + }; + try (TestHttpEndpoint endpoint = new TestHttpEndpoint("/errorToCheckBackoff", handler)) { + Http.RequestOptions options = Http.RequestOptions.builder() + .maxTries(timings.length) + .retryDelayJitterFactor(0f) + .build(); + try { + Http.get(endpoint.url(), options).get(); + } catch (Exception ignored) { + // It's ok to fail as long as it tried the expected number of times + } + assertEquals(timings.length, counter.get()); + long[] timingDeltas = new long[timings.length - 1]; + for (int i = 1; i < timings.length; i++) { + timingDeltas[i-1] = timings[i] - timings[i - 1]; + } + for (int i = 1; i < timingDeltas.length; i++) { + assertTrue(timingDeltas[i] > timingDeltas[i - 1] * 2); + } + } + } + + static class TestHttpEndpoint implements Closeable { + final InetSocketAddress address = new InetSocketAddress("127.0.0.1", randPort()); + final HttpServer server; + final String uri; + + TestHttpEndpoint(String uri, HttpHandler handler) throws IOException { + this.server = HttpServer.create(this.address, 0); + this.uri = uri; + server.createContext(uri, handler); + this.server.start(); + } + + public String url() { + return "http://" + this.address.getHostName() + ":" + this.address.getPort() + this.uri; + } + + @Override + public void close() { + this.server.stop(10); + } + + void getAndAssertStringBody(Http.RequestOptions options, String suffix, String expectedBody) throws ExecutionException, InterruptedException { + ByteBuf buffer = Http.get(this.url() + suffix).get(); + byte[] data = new byte[buffer.readableBytes()]; + buffer.readBytes(data); + String text = new String(data, StandardCharsets.UTF_8); + assertEquals(expectedBody, text); + } + void getAndAssertStringBody(String suffix, String expectedBody) throws ExecutionException, InterruptedException { + this.getAndAssertStringBody(Http.DEFAULT_REQUEST_OPTIONS, suffix, expectedBody); + } + } + + private static int randPort() { + Random random = new Random(); + return random.nextInt(65535 - 1024) + 1024; + } + +}