Try fixing occasional testing failuers in Java client

by using random and atomically increasing IDs
This commit is contained in:
xhh 2016-01-20 11:55:04 +08:00 committed by Maelig Nantel
parent ec2655165f
commit 08a2d0d941
28 changed files with 175 additions and 37 deletions

View File

@ -438,6 +438,7 @@
<module>samples/client/petstore/android/default</module>
<module>samples/client/petstore/clojure</module>
<module>samples/client/petstore/java/default</module>
<module>samples/client/petstore/java/feign</module>
<module>samples/client/petstore/java/jersey2</module>
<module>samples/client/petstore/java/okhttp-gson</module>
<module>samples/client/petstore/java/retrofit</module>

View File

@ -0,0 +1,17 @@
package io.swagger;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
public class TestUtils {
private static final AtomicLong atomicId = createAtomicId();
public static long nextId() {
return atomicId.getAndIncrement();
}
private static AtomicLong createAtomicId() {
int baseId = new Random(System.currentTimeMillis()).nextInt(1000000) + 20000;
return new AtomicLong((long) baseId);
}
}

View File

@ -1,9 +1,8 @@
package io.swagger.petstore.test;
import io.swagger.client.ApiClient;
import io.swagger.client.ApiException;
import io.swagger.client.Configuration;
import io.swagger.TestUtils;
import io.swagger.client.*;
import io.swagger.client.api.*;
import io.swagger.client.auth.*;
import io.swagger.client.model.*;
@ -203,7 +202,7 @@ public class PetApiTest {
private Pet createRandomPet() {
Pet pet = new Pet();
pet.setId(System.currentTimeMillis());
pet.setId(TestUtils.nextId());
pet.setName("gorilla");
Category category = new Category();

View File

@ -1,5 +1,7 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.ApiException;
import io.swagger.client.*;
@ -63,7 +65,7 @@ public class StoreApiTest {
private Order createOrder() {
Order order = new Order();
order.setId(new Long(System.currentTimeMillis()));
order.setId(TestUtils.nextId());
order.setPetId(new Long(200));
order.setQuantity(new Integer(13));
order.setShipDate(new java.util.Date());

View File

@ -1,5 +1,7 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.api.*;
import io.swagger.client.auth.*;
import io.swagger.client.model.*;
@ -72,7 +74,7 @@ public class UserApiTest {
private User createUser() {
User user = new User();
user.setId(System.currentTimeMillis());
user.setId(TestUtils.nextId());
user.setUsername("fred" + user.getId());
user.setFirstName("Fred");
user.setLastName("Meyer");

View File

@ -0,0 +1 @@
Hello world!

View File

@ -0,0 +1,17 @@
package io.swagger;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
public class TestUtils {
private static final AtomicLong atomicId = createAtomicId();
public static long nextId() {
return atomicId.getAndIncrement();
}
private static AtomicLong createAtomicId() {
int baseId = new Random(System.currentTimeMillis()).nextInt(1000000) + 20000;
return new AtomicLong((long) baseId);
}
}

View File

@ -1,7 +1,8 @@
package io.swagger.petstore.test;
import io.swagger.client.ApiClient;
import io.swagger.TestUtils;
import io.swagger.client.ApiClient;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
@ -183,7 +184,7 @@ public class PetApiTest {
private Pet createRandomPet() {
Pet pet = new Pet();
pet.setId(System.currentTimeMillis());
pet.setId(TestUtils.nextId());
pet.setName("gorilla");
Category category = new Category();

View File

@ -1,5 +1,9 @@
package io.swagger.petstore.test;
import feign.FeignException;
import io.swagger.TestUtils;
import io.swagger.client.*;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
@ -44,15 +48,19 @@ public class StoreApiTest {
Order fetched = api.getOrderById(order.getId().toString());
assertEquals(fetched.getId(), order.getId());
api.deleteOrder(String.valueOf(order.getId()));
api.deleteOrder(order.getId().toString());
api.getOrderById(order.getId().toString());
// fail("expected an error");
try {
api.getOrderById(order.getId().toString());
fail("expected an error");
} catch (FeignException e) {
assertTrue(e.getMessage().startsWith("status 404 "));
}
}
private Order createOrder() {
Order order = new Order();
order.setId(new Long(System.currentTimeMillis()));
order.setId(TestUtils.nextId());
order.setPetId(new Long(200));
order.setQuantity(new Integer(13));
order.setShipDate(new java.util.Date());

View File

@ -1,5 +1,7 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.ApiClient;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
@ -73,7 +75,7 @@ public class UserApiTest {
private User createUser() {
User user = new User();
user.setId(System.currentTimeMillis());
user.setId(TestUtils.nextId());
user.setUsername("fred" + user.getId());
user.setFirstName("Fred");
user.setLastName("Meyer");

View File

@ -0,0 +1,17 @@
package io.swagger;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
public class TestUtils {
private static final AtomicLong atomicId = createAtomicId();
public static long nextId() {
return atomicId.getAndIncrement();
}
private static AtomicLong createAtomicId() {
int baseId = new Random(System.currentTimeMillis()).nextInt(1000000) + 20000;
return new AtomicLong((long) baseId);
}
}

View File

@ -1,9 +1,8 @@
package io.swagger.petstore.test;
import io.swagger.client.ApiClient;
import io.swagger.client.ApiException;
import io.swagger.client.Configuration;
import io.swagger.TestUtils;
import io.swagger.client.*;
import io.swagger.client.api.*;
import io.swagger.client.auth.*;
import io.swagger.client.model.*;
@ -203,7 +202,7 @@ public class PetApiTest {
private Pet createRandomPet() {
Pet pet = new Pet();
pet.setId(System.currentTimeMillis());
pet.setId(TestUtils.nextId());
pet.setName("gorilla");
Category category = new Category();

View File

@ -1,6 +1,6 @@
package io.swagger.petstore.test;
import io.swagger.client.ApiException;
import io.swagger.TestUtils;
import io.swagger.client.*;
import io.swagger.client.api.*;
@ -63,7 +63,7 @@ public class StoreApiTest {
private Order createOrder() {
Order order = new Order();
order.setId(new Long(System.currentTimeMillis()));
order.setId(TestUtils.nextId());
order.setPetId(new Long(200));
order.setQuantity(new Integer(13));
order.setShipDate(new java.util.Date());

View File

@ -1,5 +1,7 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.api.*;
import io.swagger.client.auth.*;
import io.swagger.client.model.*;
@ -72,7 +74,7 @@ public class UserApiTest {
private User createUser() {
User user = new User();
user.setId(System.currentTimeMillis());
user.setId(TestUtils.nextId());
user.setUsername("fred" + user.getId());
user.setFirstName("Fred");
user.setLastName("Meyer");

View File

@ -0,0 +1,17 @@
package io.swagger;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
public class TestUtils {
private static final AtomicLong atomicId = createAtomicId();
public static long nextId() {
return atomicId.getAndIncrement();
}
private static AtomicLong createAtomicId() {
int baseId = new Random(System.currentTimeMillis()).nextInt(1000000) + 20000;
return new AtomicLong((long) baseId);
}
}

View File

@ -1,5 +1,7 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.*;
import io.swagger.client.api.*;
import io.swagger.client.auth.*;
@ -312,7 +314,7 @@ public class PetApiTest {
private Pet createRandomPet() {
Pet pet = new Pet();
pet.setId(System.currentTimeMillis());
pet.setId(TestUtils.nextId());
pet.setName("gorilla");
Category category = new Category();

View File

@ -1,5 +1,6 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.ApiException;
import io.swagger.client.*;
@ -67,7 +68,7 @@ public class StoreApiTest {
private Order createOrder() {
Order order = new Order();
order.setId(new Long(System.currentTimeMillis()));
order.setId(TestUtils.nextId());
order.setPetId(new Long(200));
order.setQuantity(new Integer(13));
order.setShipDate(new java.util.Date());

View File

@ -1,5 +1,7 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.api.*;
import io.swagger.client.auth.*;
import io.swagger.client.model.*;
@ -72,7 +74,7 @@ public class UserApiTest {
private User createUser() {
User user = new User();
user.setId(System.currentTimeMillis());
user.setId(TestUtils.nextId());
user.setUsername("fred" + user.getId());
user.setFirstName("Fred");
user.setLastName("Meyer");

View File

@ -0,0 +1 @@
Hello world!

View File

@ -0,0 +1,17 @@
package io.swagger;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
public class TestUtils {
private static final AtomicLong atomicId = createAtomicId();
public static long nextId() {
return atomicId.getAndIncrement();
}
private static AtomicLong createAtomicId() {
int baseId = new Random(System.currentTimeMillis()).nextInt(1000000) + 20000;
return new AtomicLong((long) baseId);
}
}

View File

@ -1,5 +1,7 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.ApiClient;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
@ -172,7 +174,7 @@ public class PetApiTest {
private Pet createRandomPet() {
Pet pet = new Pet();
pet.setId(System.currentTimeMillis());
pet.setId(TestUtils.nextId());
pet.setName("gorilla");
Category category = new Category();
@ -185,4 +187,4 @@ public class PetApiTest {
return pet;
}
}
}

View File

@ -1,5 +1,7 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.ApiClient;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
@ -56,7 +58,7 @@ public class StoreApiTest {
private Order createOrder() {
Order order = new Order();
order.setId(new Long(System.currentTimeMillis()));
order.setId(new Long(TestUtils.nextId()));
order.setPetId(new Long(200));
order.setQuantity(new Integer(13));
order.setShipDate(new java.util.Date());
@ -65,4 +67,4 @@ public class StoreApiTest {
return order;
}
}
}

View File

@ -1,5 +1,7 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.ApiClient;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
@ -70,7 +72,7 @@ public class UserApiTest {
private User createUser() {
User user = new User();
user.setId(System.currentTimeMillis());
user.setId(TestUtils.nextId());
user.setUsername("fred" + user.getId());
user.setFirstName("Fred");
user.setLastName("Meyer");
@ -81,4 +83,4 @@ public class UserApiTest {
return user;
}
}
}

View File

@ -0,0 +1 @@
Hello world!

View File

@ -0,0 +1,17 @@
package io.swagger;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
public class TestUtils {
private static final AtomicLong atomicId = createAtomicId();
public static long nextId() {
return atomicId.getAndIncrement();
}
private static AtomicLong createAtomicId() {
int baseId = new Random(System.currentTimeMillis()).nextInt(1000000) + 20000;
return new AtomicLong((long) baseId);
}
}

View File

@ -1,5 +1,7 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.ApiClient;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
@ -32,7 +34,7 @@ public class PetApiTest {
public void testCreateAndGetPet() throws Exception {
Pet pet = createRandomPet();
Response<Void> rp2 = api.addPet(pet).execute();
Response<Pet> rp = api.getPetById(pet.getId()).execute();
Pet fetched = rp.body();
assertNotNull(fetched);
@ -171,7 +173,7 @@ public class PetApiTest {
private Pet createRandomPet() {
Pet pet = new Pet();
pet.setId(System.currentTimeMillis());
pet.setId(TestUtils.nextId());
pet.setName("gorilla");
Category category = new Category();
@ -184,4 +186,4 @@ public class PetApiTest {
return pet;
}
}
}

View File

@ -1,5 +1,7 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.ApiClient;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
@ -47,12 +49,12 @@ public class StoreApiTest {
api.deleteOrder(String.valueOf(order.getId())).execute();
api.getOrderById(String.valueOf(order.getId())).execute();
//also in retrofit 1 should return an error but don't, check server api impl.
//also in retrofit 1 should return an error but don't, check server api impl.
}
private Order createOrder() {
Order order = new Order();
order.setId(new Long(System.currentTimeMillis()));
order.setId(new Long(TestUtils.nextId()));
order.setPetId(new Long(200));
order.setQuantity(new Integer(13));
order.setShipDate(new java.util.Date());
@ -61,4 +63,4 @@ public class StoreApiTest {
return order;
}
}
}

View File

@ -1,5 +1,7 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.ApiClient;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
@ -70,7 +72,7 @@ public class UserApiTest {
private User createUser() {
User user = new User();
user.setId(System.currentTimeMillis());
user.setId(TestUtils.nextId());
user.setUsername("fred");
user.setFirstName("Fred");
user.setLastName("Meyer");
@ -81,4 +83,4 @@ public class UserApiTest {
return user;
}
}
}