logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Curt Arnold <carn...@apache.org>
Subject svn commit: r500483 - in logging/log4j/trunk bug 37282
Date Tue, 30 Jan 2007 21:54:27 GMT
r500483 | eross | 2007-01-26 23:28:04 -0600 (Fri, 26 Jan 2007) | 1 line

Bug 37282 - Add real unit test for SyslogAppender, fix and test for  
file descriptor leak

Index: src/java/org/apache/log4j/helpers/SyslogWriter.java
===================================================================
113d112
<
128a128,130
 >   /**
 >    * Closes the datagram socket.
 >    */
129a132
 >     ds.close();
130a134
 >
Index: src/java/org/apache/log4j/net/SyslogAppender.java
===================================================================
128d127
<   //SyslogTracerPrintWriter stp;
130,133d128
<   private long now = -1;
<   private Date date = new Date();
<   private StringBuffer timestamp = new StringBuffer();
<   private FieldPosition pos = new FieldPosition(0);
164c159,160
<    * Release any resources held by this SyslogAppender.
---
 >    * Release resources held by this SyslogAppender,
 >    * including the datagram socket.
169,172c165
<
<     // A SyslogWriter is UDP based and needs no opening. Hence, it
<     // can't be closed. We just unset the variables here.
<     sw = null;
---
 >     sw.close();
338,347c331,333
<   void fillInTimestamp() throws IOException {
<     long n = System.currentTimeMillis();
<     n -= (n & 1000);
<
<     if ((n != now) || (timestamp.length() == 0)) {
<       now = n;
<       date.setTime(n);
<       // erase any previous value of the timestamp
<       timestamp.setLength(0);
<       sdf.format(date, timestamp, pos);
---
 >   private void writeTimestamp() throws IOException {
 >     StringBuffer timestamp = new StringBuffer();
 >     sdf.format(new Date(), timestamp, new FieldPosition(0));
353d338
<     }
357c342
<   void writeInitialParts(LoggingEvent event) throws IOException {
---
 >   private void writeInitialParts(LoggingEvent event) throws  
IOException {
362c347
<     fillInTimestamp();
---
 >     writeTimestamp();
Index: tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java
===================================================================
18a19,25
 > import java.io.IOException;
 > import java.net.DatagramPacket;
 > import java.net.DatagramSocket;
 > import java.net.ServerSocket;
 > import java.net.SocketException;
 > import java.util.StringTokenizer;
 >
26a34,35
 > import org.apache.log4j.helpers.SyslogWriter;
 > import org.apache.log4j.spi.LoggingEvent;
389d397
<
396a405,441
 >
 >   public void testActualLogging() throws IOException {
 >     DatagramSocket ds = new DatagramSocket();
 >     ds.setSoTimeout(10);
 >     SyslogAppender appender = new SyslogAppender();
 >     appender.setSyslogHost("localhost:" + ds.getLocalPort());
 >     appender.setName("name");
 >     appender.setLayout(new PatternLayout("%l %m"));
 >     appender.activateOptions();
 >
 >     Logger l = Logger.getRootLogger();
 >     l.addAppender(appender);
 >     l.info("greetings");
 >     DatagramPacket p = new DatagramPacket(new byte[1000], 0, 1000);
 >     ds.receive(p);
 >     String s = new String(p.getData(), 0, p.getLength());
 >     StringTokenizer st = new StringTokenizer(s, "<>() ");
 >     assertEquals("14", st.nextToken());
 >     assertEquals(3, st.nextToken().length());
 >     st.nextToken(); // date
 >     st.nextToken(); // time
 >     assertEquals(appender.getLocalHostname(), st.nextToken());
 >     assertEquals(getClass().getName() + ".testActualLogging",  
st.nextToken());
 >     st.nextToken(); // this filename:line number
 >     assertEquals("greetings", st.nextToken());
 >     ds.close();
 >   }
 >
 >   public void testLeak() throws IOException {
 >     DatagramSocket ds = new DatagramSocket();
 >     for (int i = 0; i < 100; i++) {
 >       SyslogWriter sw = new SyslogWriter("localhost:" +  
ds.getLocalPort());
 >       sw.close();
 >     }
 >     ds.close();
 >   }
 >


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message