/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 *
 *  http://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.
 */

package org.apache.ftpserver.clienttests;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;

import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.test.TestUtil;

/**
*
* Test for FTPSERVER-170
*
* @author The Apache MINA Project (dev@mina.apache.org)
* @version $Rev$, $Date$
*
*/
public class DataTransferTimeoutTest extends ClientTestTemplate {
    private static final String TEST_FILENAME = "test.txt";
    
    private static final File TEST_FILE = new File(ROOT_DIR, TEST_FILENAME);

    @Override
    protected FtpServerFactory createServer() throws Exception {
        FtpServerFactory serverFactory = super.createServer();
        
        // set a really short timeout
        ListenerFactory listenerFactory = new ListenerFactory(serverFactory.getListener("default"));
        listenerFactory.setIdleTimeout(1);
        
        serverFactory.addListener("default", listenerFactory.createListener());
        
        return serverFactory;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.ftpserver.clienttests.ClientTestTemplate#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();

        client.login(ADMIN_USERNAME, ADMIN_PASSWORD);
    }

    public void testTimeoutForStore() throws Exception {
        OutputStream os = client.storeFileStream(TEST_FILENAME);
        
        os.write(1);
        
        // make sure this take longer than the timeout time, but not timeout between writes...
        for(int i = 0; i<100; i++) {
            Thread.sleep(20);
            os.write(1);   
            os.flush();
        }
        
        os.close();

        client.completePendingCommand();
        
        // we should not have been disconnected
        client.noop();
    }

    public void testTimeoutForRetreive() throws Exception {
        // as used by IODataConnection
        int bufferSize = 4096 * 10;
        byte[] buffer = new byte[bufferSize];
        
        byte[] testData = new byte[200 * bufferSize];
        Arrays.fill(testData, (byte)1);
        
        TestUtil.writeDataToFile(TEST_FILE, testData);
        InputStream is = client.retrieveFileStream(TEST_FILENAME);

        // read one buffer size at a time, trying to trigger IODataConnection to update
        // the session timeout for each read
        for(int i = 0; i<100; i++) {
            Thread.sleep(20);
            is.read(buffer);   
        }
        
        is.close();

        client.completePendingCommand();
        
        // we should not have been disconnected
        client.noop();
    }
}

@@ -3110,19 +3110,19 @@
 // read 
-one
+ten
  buffer 
@@ -3125,16 +3125,17 @@
 fer size
+s
  at a ti
@@ -3281,57 +3281,262 @@
     
-Thread.sleep(20);%0A            is.read(buffer);   
+long startTime = System.currentTimeMillis();%0A            Thread.sleep(20);%0A            is.read(buffer);%0A            %0A            if((System.currentTimeMillis() - startTime) %3E 500 ) %7B%0A                fail(%22Read took to long, test not safe%22);%0A            %7D
 %0A   

Test still failing in the CI build, adding a check