Skip to content

Commit b5266db

Browse files
committed
Add getActiveCount() to return active ajax threads.
1 parent f599463 commit b5266db

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

src/com/androidquery/callback/AbstractAjaxCallback.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Map;
3939
import java.util.concurrent.ExecutorService;
4040
import java.util.concurrent.Executors;
41+
import java.util.concurrent.ThreadPoolExecutor;
4142
import java.util.regex.Matcher;
4243
import java.util.regex.Pattern;
4344
import java.util.zip.GZIPInputStream;
@@ -1279,6 +1280,24 @@ public static void execute(Runnable job){
12791280
fetchExe.execute(job);
12801281
}
12811282

1283+
/**
1284+
* Return the number of active ajax threads. Note that this doesn't necessarily correspond to active network connections.
1285+
* Ajax threads might be reading a cached url from file system or transforming the response after a network transfer.
1286+
*
1287+
*/
1288+
1289+
public static int getActiveCount(){
1290+
1291+
int result = 0;
1292+
1293+
if(fetchExe instanceof ThreadPoolExecutor){
1294+
result = ((ThreadPoolExecutor) fetchExe).getActiveCount();
1295+
}
1296+
1297+
return result;
1298+
1299+
}
1300+
12821301
/**
12831302
* Sets the simultaneous network threads limit. Highest limit is 25.
12841303
*

tests/src/com/androidquery/test/AQueryAsyncTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,5 +1644,32 @@ public void callback(String url, JSONObject jo, AjaxStatus status) {
16441644
assertNotNull(file);
16451645

16461646

1647+
}
1648+
1649+
1650+
public void testAjaxActiveCount() {
1651+
1652+
assertEquals(0, AjaxCallback.getActiveCount());
1653+
1654+
1655+
String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
1656+
1657+
aq.ajax(url, JSONObject.class, this, "jsonCb");
1658+
1659+
1660+
int count = AjaxCallback.getActiveCount();
1661+
AQUtility.debug("active count", count);
1662+
1663+
assertEquals(1, AjaxCallback.getActiveCount());
1664+
1665+
waitAsync();
1666+
1667+
assertEquals(0, AjaxCallback.getActiveCount());
1668+
1669+
JSONObject jo = (JSONObject) result;
1670+
1671+
assertNotNull(jo);
1672+
assertNotNull(jo.opt("responseData"));
1673+
16471674
}
16481675
}

tests/src/com/androidquery/test/AQueryImageTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ public void run() {
474474

475475
Bitmap bm = aq.getCachedImage(R.drawable.icon);
476476
assertNotNull(bm);
477+
478+
477479
}
478480
});
479481

@@ -483,4 +485,25 @@ public void run() {
483485

484486
}
485487

488+
public void testIfModified() {
489+
490+
String url = ICON_URL;
491+
492+
AjaxCallback<Bitmap> cb = new AjaxCallback<Bitmap>();
493+
cb.type(Bitmap.class).url(url);
494+
495+
aq.sync(cb);
496+
497+
Bitmap bm = cb.getResult();
498+
AjaxStatus status = cb.getStatus();
499+
500+
assertNotNull(bm);
501+
502+
assertEquals(304, status.getCode());
503+
504+
File file;
505+
506+
507+
}
508+
486509
}

0 commit comments

Comments
 (0)