Search in sources :

Example 26 with Submission

use of cn.edu.zju.acm.onlinejudge.bean.Submission in project zoj by licheng.

the class JudgeQueueUnitTest method testRejudge.

@Test(timeout = 1000)
public void testRejudge() throws Exception {
    queue.add(submissions[0]);
    queue.add(submissions[1]);
    queue.add(submissions[2]);
    Submission submission = queue.removeFirst();
    queue.addFirst(queue.removeFirst());
    queue.addFirst(submission);
    assertEquals((long) 1, queue.removeFirst().getId());
    assertEquals((long) 0, queue.removeFirst().getId());
    assertEquals((long) 2, queue.removeFirst().getId());
}
Also used : Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) Test(org.junit.Test)

Example 27 with Submission

use of cn.edu.zju.acm.onlinejudge.bean.Submission in project zoj by licheng.

the class JudgeQueueUnitTest method testRestoreFromDAO.

@Test
public void testRestoreFromDAO() throws Exception {
    for (int i = 0; i < 1000; i++) {
        Submission submission = new Submission();
        submission.setContent("" + i);
        submissionDAO.save(submission);
        char[] content = new char[1024 * 128];
        Arrays.fill(content, (char) i);
        submission.setContent(new String(content));
        queue.add(submission);
    }
    for (int i = 0; i < 1000; i++) {
        Submission submission = queue.removeFirst();
        assertEquals((long) i, submission.getId());
    }
}
Also used : Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) Test(org.junit.Test)

Example 28 with Submission

use of cn.edu.zju.acm.onlinejudge.bean.Submission in project zoj by licheng.

the class JudgingQueueUnitTest method testMultipleThreads.

/**
     * This test works in this way: We first save a global iterator, then create a set of judge threads which add and
     * remove submissions and another set of check threads which get iterators from the queue. We finally assert that
     * every submission sequence seen by a check thread is a sub-sequence of that returned by the global iterator saved
     * before. In order to be efficient, we do not save all submission sequence seen by check threads. Instead, we save the
     * hash code of submission ids, the first submission id and total number of submissions in the sequence.
     */
@Test
public void testMultipleThreads() {
    JudgingQueueIterator allIter = queue.iterator();
    final Thread[] judge = new Thread[50];
    final int maxIdPerJudgeThread = 1000;
    for (int i = 0; i < judge.length; ++i) {
        final int id = i;
        judge[i] = new Thread() {

            public void run() {
                for (int i = 0; i < maxIdPerJudgeThread; ++i) {
                    Submission submission = new Submission();
                    submission.setId(id * maxIdPerJudgeThread + i);
                    queue.push(submission);
                    Thread.yield();
                    queue.remove(submission);
                }
            }
        };
        judge[i].start();
    }
    Thread[] check = new Thread[100];
    final long[] start = new long[check.length];
    final int[] len = new int[check.length];
    final long[] hash = new long[check.length];
    for (int i = 0; i < check.length; ++i) {
        final int id = i;
        check[i] = new Thread() {

            public void run() {
                hash[id] = len[id] = 0;
                JudgingQueueIterator iter = queue.iterator();
                for (int i = 0; i < 100; ++i) {
                    Submission submission = iter.next();
                    if (submission != null) {
                        start[id] = hash[id] = submission.getId();
                        break;
                    }
                    Thread.yield();
                }
                if (len[id] > 0) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    }
                    for (Submission submission = iter.next(); submission != null; submission = iter.next()) {
                        ++len[id];
                        hash[id] = hash[id] * 31 + submission.getId();
                    }
                }
            }
        };
        check[i].start();
    }
    for (int i = 0; i < judge.length; ++i) {
        try {
            judge[i].join();
        } catch (InterruptedException e) {
        }
    }
    for (int i = 0; i < check.length; ++i) {
        try {
            check[i].join();
        } catch (InterruptedException e) {
        }
    }
    List<Long> h = new ArrayList<Long>();
    List<Long> base = new ArrayList<Long>();
    Map<Long, Integer> m = new HashMap<Long, Integer>();
    long[] last = new long[judge.length];
    for (int i = 0; i < last.length; ++i) {
        last[i] = -1;
    }
    h.add(0L);
    base.add(1L);
    for (Submission submission = allIter.next(); submission != null; submission = allIter.next()) {
        long id = submission.getId();
        m.put(id, h.size());
        h.add(h.get(h.size() - 1) * 31 + id);
        base.add(base.get(base.size() - 1) * 31);
        int a = (int) (id / maxIdPerJudgeThread);
        long b = id % maxIdPerJudgeThread;
        if (last[a] < 0) {
            last[a] = b;
        } else {
            assertThat(b, is(last[a] + 1));
            last[a] = b;
        }
    }
    for (int i = 0; i < check.length; ++i) {
        if (len[i] > 0) {
            int s = m.get(start[i]);
            assertThat(hash[i], is(h.get(s + len[i] - 1) - h.get(s - 1) * base.get(len[i])));
        }
    }
}
Also used : Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 29 with Submission

use of cn.edu.zju.acm.onlinejudge.bean.Submission in project zoj by licheng.

the class SubmissionPersistenceImpl method populateSubmission.

/**
     * Populates an ExtendedSubmission with given ResultSet.
     * 
     * @param rs
     * @return an ExtendedSubmission instance
     * @throws SQLException
     */
private Submission populateSubmission(ResultSet rs, boolean withContent, Map<Long, Language> languageMap) throws SQLException {
    Submission submission = new Submission();
    submission.setId(rs.getLong(DatabaseConstants.SUBMISSION_SUBMISSION_ID));
    submission.setProblemId(rs.getLong(DatabaseConstants.SUBMISSION_PROBLEM_ID));
    submission.setUserProfileId(rs.getLong(DatabaseConstants.SUBMISSION_USER_PROFILE_ID));
    submission.setJudgeComment(rs.getString(DatabaseConstants.SUBMISSION_JUDGE_COMMENT));
    submission.setJudgeDate(Database.getDate(rs, DatabaseConstants.SUBMISSION_JUDGE_DATE));
    submission.setSubmitDate(Database.getDate(rs, DatabaseConstants.SUBMISSION_SUBMISSION_DATE));
    submission.setMemoryConsumption(rs.getInt(DatabaseConstants.SUBMISSION_MEMORY_CONSUMPTION));
    submission.setTimeConsumption(rs.getInt(DatabaseConstants.SUBMISSION_TIME_CONSUMPTION));
    submission.setUserName(rs.getString(DatabaseConstants.USER_PROFILE_NICKNAME));
    if (submission.getUserName().equals("")) {
        submission.setUserName(rs.getString(DatabaseConstants.USER_PROFILE_HANDLE));
    }
    submission.setProblemCode(rs.getString(DatabaseConstants.PROBLEM_CODE));
    submission.setContestId(rs.getLong("contest_id"));
    submission.setContestOrder(rs.getLong("contest_order"));
    if (withContent) {
        submission.setContent(rs.getString("content"));
    }
    // set language
    long languageId = rs.getLong(DatabaseConstants.SUBMISSION_LANGUAGE_ID);
    Language language = languageMap.get(languageId);
    submission.setLanguage(language);
    // set judge reply
    long judgeReplyId = rs.getLong(DatabaseConstants.SUBMISSION_JUDGE_REPLY_ID);
    JudgeReply judgeReply = JudgeReply.findById(judgeReplyId);
    submission.setJudgeReply(judgeReply);
    return submission;
}
Also used : Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) JudgeReply(cn.edu.zju.acm.onlinejudge.bean.enumeration.JudgeReply)

Example 30 with Submission

use of cn.edu.zju.acm.onlinejudge.bean.Submission in project zoj by licheng.

the class SubmissionPersistenceImpl method getSubmission.

/**
     * <p>
     * Gets the submission with given id in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the submission
     * @return the submission with given id in persistence layer
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public Submission getSubmission(long id) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(SubmissionPersistenceImpl.GET_SUBMISSION.replace("FORCE_INDEX", ""));
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (!rs.next()) {
                return null;
            }
            Map<Long, Language> languageMap = PersistenceManager.getInstance().getLanguagePersistence().getLanguageMap();
            Submission submission = this.populateSubmission(rs, true, languageMap);
            return submission;
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the submission with id " + id, e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Submission (cn.edu.zju.acm.onlinejudge.bean.Submission)33 Test (org.junit.Test)8 ExtendedSubmission (cn.edu.zju.acm.onlinejudge.bean.ExtendedSubmission)7 Language (cn.edu.zju.acm.onlinejudge.bean.enumeration.Language)7 ArrayList (java.util.ArrayList)5 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)4 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 Date (java.util.Date)4 ActionForward (org.apache.struts.action.ActionForward)4 Before (org.junit.Before)4 Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)3 SubmissionPersistence (cn.edu.zju.acm.onlinejudge.persistence.SubmissionPersistence)3 ActionMessages (org.apache.struts.action.ActionMessages)3 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)2 Reference (cn.edu.zju.acm.onlinejudge.bean.Reference)2 List (java.util.List)2