// 설명 문서: /docs/specs/bada-bong-direct-observation.md (v0.1.0) // 자체 loopback HTTP 서버와 실제 생산 Java 모듈만 사용한다. Android/모델 호출은 없다. import ai.opencode.bong.net.BongHttpRequests; import com.sun.net.httpserver.HttpServer; import java.net.*; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; public class BongHttpRequestsTest { static void check(boolean value,String code) { if(!value)throw new AssertionError(code); } static void waitFor(java.util.function.BooleanSupplier predicate) throws Exception { long end=System.nanoTime()+TimeUnit.SECONDS.toNanos(5); while(!predicate.getAsBoolean()) { if(System.nanoTime()>end)throw new AssertionError("TIMEOUT");Thread.sleep(10); } } public static void main(String[] args) throws Exception { HttpServer server=HttpServer.create(new InetSocketAddress("127.0.0.1",0),0); ExecutorService serving=Executors.newCachedThreadPool();server.setExecutor(serving); Set received=ConcurrentHashMap.newKeySet();CountDownLatch releaseServer=new CountDownLatch(1); server.createContext("/",exchange->{ try { String input=new String(exchange.getRequestBody().readAllBytes(),StandardCharsets.UTF_8);received.add(input); if(exchange.getRequestURI().getPath().equals("/hang")) { exchange.sendResponseHeaders(200,0);exchange.getResponseBody().write('x');exchange.getResponseBody().flush();releaseServer.await(10,TimeUnit.SECONDS);return; } byte[] body=exchange.getRequestURI().getPath().equals("/large")?new byte[4194305]:"{\"text\":\"한글 응답\"}".getBytes(StandardCharsets.UTF_8); exchange.sendResponseHeaders(exchange.getRequestURI().getPath().equals("/error")?429:200,body.length);exchange.getResponseBody().write(body); }catch(Exception ignored){/* 취소로 닫힌 fixture 소켓 */}finally{exchange.close();} });server.start();String base="http://127.0.0.1:"+server.getAddress().getPort(); int checks=0; try { BongHttpRequests requests=new BongHttpRequests();BlockingQueue replies=new LinkedBlockingQueue<>(); BongHttpRequests.Callback callback=(result,release)->{replies.add(result);release.run();}; try { check(requests.submit("ok",base+"/ok","INPUT",Collections.emptyMap(),callback),"ACCEPT"); BongHttpRequests.Result result=replies.poll(5,TimeUnit.SECONDS);check(result!=null&&result.status.equals("response")&&result.httpStatus==200&&result.body.contains("한글"),"RESPONSE");checks++; check(requests.submit("error",base+"/error","ERROR",Collections.emptyMap(),callback),"ERROR_ACCEPT");result=replies.poll(5,TimeUnit.SECONDS);check(result!=null&&result.httpStatus==429&&result.status.equals("response"),"HTTP_STATUS");checks++; check(requests.submit("large",base+"/large","LARGE",Collections.emptyMap(),callback),"LARGE_ACCEPT");result=replies.poll(5,TimeUnit.SECONDS);check(result!=null&&result.code.equals("HTTP_RESPONSE_LIMIT")&&result.body==null,"BOUND_RESPONSE");checks++; check(!requests.submit("bad id",base,"",Collections.emptyMap(),callback),"ID");check(!requests.submit("scheme","file:///tmp/fixture","",Collections.emptyMap(),callback),"SCHEME");check(!requests.submit("body",base,"한".repeat(400000),Collections.emptyMap(),callback),"UTF8_BOUND");check(!requests.submit("headers",base,"",Collections.singletonMap("x", "a".repeat(16385)),callback),"HEADER_BOUND");checks++; check(requests.submit("callback",base,"THROW",Collections.emptyMap(),(r,release)->{throw new IllegalStateException("fixture");}),"CALLBACK_ACCEPT");waitFor(()->received.contains("THROW")&&requests.activeCount()==0);checks++; }finally{requests.close();check(requests.awaitTermination(5000),"TERMINATE");} BongHttpRequests held=new BongHttpRequests();BlockingQueue releases=new LinkedBlockingQueue<>(); try { for(int i=0;i<4;i++)check(held.submit("held"+i,base,"HELD"+i,Collections.emptyMap(),(r,release)->releases.add(release)),"HELD_ACCEPT");waitFor(()->releases.size()==4); check(held.activeCount()==4&&!held.submit("overflow",base,"",Collections.emptyMap(),callback),"UI_QUEUE_BOUND");check(!held.cancel("held0"),"FINISHED_CANCEL"); for(Runnable release:releases)release.run();waitFor(()->held.activeCount()==0);checks++; }finally{held.close();check(held.awaitTermination(5000),"HELD_TERMINATE");} BongHttpRequests cancelling=new BongHttpRequests(1000,1500); try { check(cancelling.submit("active0",base+"/hang","ACTIVE0",Collections.emptyMap(),callback),"ACTIVE0");check(cancelling.submit("active1",base+"/hang","ACTIVE1",Collections.emptyMap(),callback),"ACTIVE1");waitFor(()->received.contains("ACTIVE0")&&received.contains("ACTIVE1")); check(cancelling.submit("queued0",base+"/hang","QUEUED0",Collections.emptyMap(),callback),"QUEUE0");check(cancelling.submit("queued1",base+"/hang","QUEUED1",Collections.emptyMap(),callback),"QUEUE1");check(!cancelling.submit("overflow",base,"",Collections.emptyMap(),callback),"CAP"); long start=System.nanoTime();check(cancelling.cancel("queued0"),"CANCEL_QUEUE");check(cancelling.cancel("active0"),"CANCEL_ACTIVE");check(TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-start)<500,"CANCEL_NONBLOCKING");check(cancelling.activeCount()>=2,"ACTIVE_LEASE_BEFORE_CLEANUP"); Set cancelled=new HashSet<>();while(cancelled.size()<2){BongHttpRequests.Result result=replies.poll(5,TimeUnit.SECONDS);check(result!=null,"CANCEL_ACK");if(result.id.equals("active0")||result.id.equals("queued0")){check(result.status.equals("cancelled"),"CANCEL_STATUS");cancelled.add(result.id);}else check(result.status.equals("failed"),"OTHER_TIMEOUT");}check(cancelled.containsAll(Arrays.asList("queued0","active0")),"EXACT_CANCEL");check(!received.contains("QUEUED0"),"QUEUED_NOT_SENT");checks++; cancelling.close();releaseServer.countDown();check(cancelling.awaitTermination(5000),"CLOSE_TERMINATE");check(cancelling.activeCount()==0,"CLOSE_LEASES");check(!cancelling.submit("after",base,"",Collections.emptyMap(),callback),"CLOSED_ADMISSION");checks++; }finally{releaseServer.countDown();cancelling.close();} System.out.println("PASS "+checks+" native HTTP lifecycle checks"); }finally{releaseServer.countDown();server.stop(0);serving.shutdownNow();check(serving.awaitTermination(5000,TimeUnit.MILLISECONDS),"SERVER_CLEANUP");} } }