changeset 812:43eb11e7f86b

test in progress
author Devel 1
date Mon, 15 Jan 2018 09:23:05 +0100
parents a6ca1dda7e52
children 3a7411ab09e9
files stress-tester/src/main/java/com/passus/st/client/http/HttpSynchClientWorker.java stress-tester/src/main/java/com/passus/st/emitter/nio/NioChannelContext.java stress-tester/src/main/java/com/passus/st/emitter/nio/NioDefaultEmitterWorker.java stress-tester/src/main/java/com/passus/st/emitter/nio/NioEmitterWorker.java stress-tester/src/main/java/com/passus/st/emitter/nio/NioEmitterWorker2.java stress-tester/src/test/java/com/passus/st/client/http/HttpSynchClientWorkerTest.java
diffstat 6 files changed, 135 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/HttpSynchClientWorker.java	Mon Jan 08 12:33:40 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/HttpSynchClientWorker.java	Mon Jan 15 09:23:05 2018 +0100
@@ -131,6 +131,10 @@
         }
     }
 
+    /**
+     * Returns true if next event should be processed immediately.
+     * @return 
+     */
     private boolean pollNext() {
         if (currFlowContext != null) {
             return false;
@@ -155,8 +159,8 @@
                         try {
                             currFlowContext = register(statusEvent);
                             if (currFlowContext != null) {
+                                currFlowContext.setParameters(currentFlowParameters);
                                 emitter.connect(statusEvent.getSessionInfo(), this, index);
-                                currFlowContext.setParameters(currentFlowParameters);
                             }
                         } catch (Exception e) {
                             logger.error(e.getMessage(), e);
--- a/stress-tester/src/main/java/com/passus/st/emitter/nio/NioChannelContext.java	Mon Jan 08 12:33:40 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/emitter/nio/NioChannelContext.java	Mon Jan 15 09:23:05 2018 +0100
@@ -45,7 +45,7 @@
         return dataQueue;
     }
 
-    void selectionKey(SelectionKey key) {
+    public void selectionKey(SelectionKey key) {
         this.key = key;
     }
 
--- a/stress-tester/src/main/java/com/passus/st/emitter/nio/NioDefaultEmitterWorker.java	Mon Jan 08 12:33:40 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/emitter/nio/NioDefaultEmitterWorker.java	Mon Jan 15 09:23:05 2018 +0100
@@ -68,7 +68,7 @@
     }
 
     @Override
-    void flush(SelectionKey key) {
+    protected void flush(SelectionKey key) {
         tasks.add(new FlushTask(key));
         selector.wakeup();
     }
@@ -401,7 +401,7 @@
     }
 
     @Override
-    void requestClose(SelectionKey key) {
+    protected void requestClose(SelectionKey key) {
         tasks.add(new CloseTask(key));
         key.selector().wakeup();
     }
--- a/stress-tester/src/main/java/com/passus/st/emitter/nio/NioEmitterWorker.java	Mon Jan 08 12:33:40 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/emitter/nio/NioEmitterWorker.java	Mon Jan 15 09:23:05 2018 +0100
@@ -44,9 +44,9 @@
 
     public abstract void setWorking(boolean b);
 
-    abstract void flush(SelectionKey key);
+    protected abstract void flush(SelectionKey key);
 
-    abstract void requestClose(SelectionKey key);
+    protected abstract void requestClose(SelectionKey key);
 
     public long getSelectTimeout() {
         return selectTimeout;
--- a/stress-tester/src/main/java/com/passus/st/emitter/nio/NioEmitterWorker2.java	Mon Jan 08 12:33:40 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/emitter/nio/NioEmitterWorker2.java	Mon Jan 15 09:23:05 2018 +0100
@@ -139,7 +139,7 @@
     }
 
     @Override
-    void flush(SelectionKey key) {
+    protected void flush(SelectionKey key) {
         tasks.add(new FlushTask(key));
         selector.wakeup();
     }
@@ -634,7 +634,7 @@
     }
 
     @Override
-    void requestClose(SelectionKey key) {
+    protected void requestClose(SelectionKey key) {
         tasks.add(new CloseTask(key));
         wakeUp();
     }
--- a/stress-tester/src/test/java/com/passus/st/client/http/HttpSynchClientWorkerTest.java	Mon Jan 08 12:33:40 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/client/http/HttpSynchClientWorkerTest.java	Mon Jan 15 09:23:05 2018 +0100
@@ -1,6 +1,11 @@
 package com.passus.st.client.http;
 
 import com.passus.config.Configuration;
+import com.passus.data.ByteBuff;
+import com.passus.data.HeapByteBuff;
+import com.passus.net.http.HttpRequest;
+import com.passus.net.http.HttpResponse;
+import com.passus.net.http.HttpResponseEncoder;
 import com.passus.st.Log4jConfigurationFactory;
 import com.passus.st.client.Event;
 import com.passus.st.client.SessionEvent;
@@ -13,9 +18,13 @@
 import com.passus.st.emitter.SessionInfo;
 import com.passus.st.emitter.SessionMapper;
 import com.passus.st.emitter.nio.NioChannelContext;
+import com.passus.st.emitter.nio.NioEmitterWorker;
 import com.passus.st.metric.MetricsContainer;
 import com.passus.st.utils.EventUtils;
 import java.io.IOException;
+import java.nio.channels.SelectableChannel;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Properties;
@@ -29,6 +38,53 @@
  */
 public class HttpSynchClientWorkerTest {
 
+    private static class LocalEmitterWorker extends NioEmitterWorker {
+
+        public LocalEmitterWorker(int index) throws IOException {
+            super(index);
+        }
+
+        @Override
+        public void connect(SessionInfo sessionInfo, EmitterHandler handler) throws IOException {
+            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+        }
+
+        @Override
+        public void setWorking(boolean b) {
+            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+        }
+
+        @Override
+        protected void flush(SelectionKey key) {
+            KeyContext keyContext = (KeyContext) key.attachment();
+            SessionInfo sessionInfo = keyContext.channelContext.getSessionInfo();
+            HttpFlowBasedClientWorker clientWorker = (HttpFlowBasedClientWorker) keyContext.handler;
+            HttpFlowContext flowContext = clientWorker.flowContext(sessionInfo);
+            HttpSessionPayloadEvent event = flowContext.sentEvent;
+            HttpRequest request = event.getRequest();
+            HttpResponse response = event.getResponse();
+            ByteBuff buff = new HeapByteBuff();
+            HttpResponseEncoder.INSTANCE.encode(response, buff);
+            try {
+                keyContext.handler.dataReceived(keyContext.channelContext, buff);
+            } catch (Exception ex) {
+                ex.printStackTrace();
+            }
+            System.out.println("flush");
+        }
+
+        @Override
+        protected void requestClose(SelectionKey key) {
+            System.out.println("requestClose");
+        }
+
+        @Override
+        public void writeMetrics(MetricsContainer container) {
+            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+        }
+
+    }
+
     private static class LocalEmitter implements Emitter {
 
         private SessionMapper sessionMapper = new PassThroughSessionMapper();
@@ -52,12 +108,16 @@
             // doConnect -> channelRegistered
             // selector
             // doFinishConnect -> channelActive
-            ChannelContext context = new NioChannelContext(null, null, null, session);
+            NioEmitterWorker worker = new LocalEmitterWorker(workerIndex);
+            NioChannelContext channelContext = new NioChannelContext(worker, null, null, session);
             try {
-                handler.channelRegistered(context);
-                handler.channelActive(context);
+                handler.channelRegistered(channelContext);
+                handler.channelActive(channelContext);
+                KeyContext keyContext = new KeyContext(channelContext, handler);
+                SelectionKey key = selectionKey(keyContext);
+                channelContext.selectionKey(key);
             } catch (Exception ex) {
-
+                ex.printStackTrace();
             }
         }
 
@@ -94,6 +154,20 @@
         }
     }
 
+    private static class KeyContext {
+
+        private final EmitterHandler handler;
+
+        private final ChannelContext channelContext;
+
+        public KeyContext(ChannelContext channelContext, EmitterHandler handler) {
+            this.channelContext = channelContext;
+            this.handler = handler;
+        }
+
+    }
+
+    // wisi w pętli pollNext
     @Test(enabled = false)
     public void testHandle1() throws Exception {
         Log4jConfigurationFactory.enableFactory(Level.DEBUG);
@@ -118,4 +192,49 @@
 
         System.out.println("");
     }
+
+    private static SelectionKey selectionKey(Object attachment) {
+        SelectionKey key = new XSelectionKey();
+        key.attach(attachment);
+        return key;
+    }
+
+    private static class XSelectionKey extends SelectionKey {
+
+        @Override
+        public SelectableChannel channel() {
+            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+        }
+
+        @Override
+        public Selector selector() {
+            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+        }
+
+        @Override
+        public boolean isValid() {
+            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+        }
+
+        @Override
+        public void cancel() {
+            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+        }
+
+        @Override
+        public int interestOps() {
+            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+        }
+
+        @Override
+        public SelectionKey interestOps(int ops) {
+            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+        }
+
+        @Override
+        public int readyOps() {
+            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+        }
+
+    }
 }