Author Archives: whyjava

Working through sbt test deadlock

Today, I encountered an issue while running tests for one of my Scala SBT projects. Each time, ran sbt test command hang. After running jvisualvm, I discovered that it is due to thread deadlock. I couldn’t figure out why deadlock is happening. Test cases worked fine when ran individually. To work through this issue, I disabled parallel execution of tests.

From command-line, you can use following command to disable parallel execution of test:

$ sbt 'set parallelExecution in Test := false' test

You can also set this setting in your build.sbt to avoid setting this setting manually.In your build.sbt , add the following line.

parallelExecution := false

Hands-on guide for building Serverless applications

Yesterday, I released hands-on guide to building Serverless applications using AWS Lambda and Serverless framework. The guide is open-source and available on Github. Checkout the guide and please give feedback.

Serverless is an overloaded word. Serverless means different things depending on the context. It could mean using third party managed services like Firebase, or it could mean an event driven architecture style or it could mean next generation compute service offered by cloud providers or it could mean a framework to build Serverless applications. This series will start with an introduction to Serverless compute and architecture. Once we learned the basics, we will start developing application in a step by manner.

Read more https://github.com/shekhargulati/hands-on-serverless-guide.

Why it’s hard for programmers to write a program to flatten a list?

I take many programming interviews at my current organisation. These days one of my favourite interview question is to flatten a nested list structure. I give user an input [1,[2,3], [4, [5,6]]] and ask them to write a program that will give output [1,2,3,4,5,6]. So, far I have asked this question to at least 20 candidates and 18 out of them have failed to write this program. I find this question better than canonical FizzBuzz problem as flattening a list is comparatively more involved problem.

This is how interview goes:

  1. I ask candidate to write a program that takes [1,[2,3], [4, [5,6]]] as input and return [1,2,3,4,5,6] as output. I specifically say you have to flatten a list.
  2. They can choose any programming language they like. Usually they are Java programmers so they end up choosing that.
  3. I ask them to write test case as well.

The common patterns I have noticed in most interviews:

  1. Candidate fail to write proper method signature. They get confused about what type of list they should use. Some start with List of integers List<Integer> ints. They fail to see how they will store a List<Integer> to a List<Integer>.
    public List<Integer> flatten(List<Integer> numbers)
  2. Some candidates start with a primitive array. Very quickly they realize array is fixed size data structure so they should use a List.
  3. They give method bad name like getList or processList etc. While explaining the problem, I explicitly mention that they have to flatten a list still they don’t use flatten as name of the method.
  4. Most programmers fail to get that they can recursion to solve the problem. Candidates who solved this problem get that they have to use recursion in a few seconds.
  5. Some programmers have hard time storing result in a List using the recursion approach. They use recursion but forget to store result. Most don’t know difference between recursion and tail recursion.
  6. Some programmers have hard time thinking how they should check instance is of some type.
  7. Most of them make flatten instance method. Programmers don’t think whether they should make method static. Programmers for some reason think static is bad.
  8. No one starts with a test. Most jump straight away to code. You have to pause them and ask can you please write down in plain English what you are trying to achieve.
  9. I tend to give a lot of help and pointers to the candidate. I want to make sure that they solve this problem. Candidates end up taking close to 45-60 mins to finish the solution. Some fail to get it working even after an hour.
  10. No one thinks about generic program so that solution will work across all types.
  11. No one used Java 8. Candidates tell they have heard some features of Java 8 but they don’t use it. I doubt adoption of Java 8 has reached a critical mass.

I am not sure what makes this problem tough for candidates. May be pair programming with interviewer just does not work. What are your thoughts?

OkHttp with Let’s Encrypt Certificates

If you are using OKHttp to make request to a server that used Let’s Encrypt certificate then you might get following exception.

&lt;br /&gt;Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1506)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:241)
at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:198)
at okhttp3.internal.connection.RealConnection.buildConnection(RealConnection.java:174)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:114)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:193)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:129)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:98)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
at okhttp3.RealCall.execute(RealCall.java:60)
at com.shekhargulati.thereader.readers.FeedReader.readFeed(FeedReader.java:22)
... 28 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
at sun.security.validator.Validator.validate(Validator.java:260)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1488)
... 56 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:146)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
... 62 more

One way to avoid this error is by using JDK 1.8.0_101. This will make your code run fine. If upgrading is not an option, then you can try to add certificates to keystore by following this stackoverflow post.

Be a Rock Star at Work

Yesterday, I started reading The Greatness Guide book by Robin Sharma and one of the important lessons he shared is Be a Rock Star at Work. This looks obvious but if you ask yourself honestly you will be surprised how many times you failed to live up to your own expectations. We all want to do best at our workplaces but fail to make an impact or do justice to our work. This could be because you don’t enjoy your work or your workplace sucks. At the end, we end up just being mediocre rather than achieving to our full potential. We waste a hell lot of time in office politics or tea time gossips that pollute our mind so much that we don’t give our 100% at work. Our mind is always occupied by so many other thoughts that we never do justice to our work. This is a very important lesson as work is what defines us and provide meaning to our life. When you go back home after office day you should be satisfied that you gave your 100% to the problem at hand.